Skip to main content

A declarative approach to exception handling in python

Project description

Safe Callback

About

safe-callback is a simple library that provides a way to safely call a function or method and handle anticipated errors in a customizable way. It is designed to be used in situations where a function or method may fail, but the failure is not fatal and should be handled gracefully. However, this situation seems to be common enough that error handling code can become repetitive and clutter the main logic of a program. safe-callback aims to simplify this by providing a way to handle errors in a predictable and customizable way.

Installation

Install safe-callback using pip:

$ pip install safe-callback

Usage

safe-callback uses a decorator to wrap a function or method to handle any anticipated errors that may occur in the wrapped function or method. The decorator can specify a mapping of which errors to handle and how to handle them. If an instance of the exception is raised, the corresponding handler will be called. If no handler is specified for a particular exception, the exception will be raised as normal.

Basic Usage

Use the @safecallback decorator to wrap a function or method. Specify the anticipated exceptions and how to handle them as a dictionary.

from safe_callback import safecallback

@safecallback({
  ZeroDivisionError: lambda e: print("Denominator cannot be zero")
})
def divide(numerator, denominator):
  return numerator / denominator

In this example, the divide function is protected from a ZeroDivisionError. If a ZeroDivisionError is raised, the provided lambda function is called and the exception is handled. If any other exception is raised, it will be raised as normal.

>>> divide(4, 2) # Returns 2.0 as normal
2.0
>>> divide(4, 0) # Prints "Denominator cannot be zero" and returns None
Denominator cannot be zero

Advanced Usage

The @safecallback decorator can also be used without any arguments. In this case, the wrapped function or method will not handle any exceptions, but will allow the exception handlers to be specified at a later time using the error_handler sub-decorator. This can be useful when the exception mapping grows too large or for specifying exception handlers at runtime.

@safecallback() # Exception mapping is empty, so no exceptions will be handled
def divide(numerator, denominator):
  return numerator / denominator

# Elsewhere or maybe in some other module
@divide.error_handler(ZeroDivisionError)
def handle_zero_division_error(e):
  print("Denominator cannot be zero")

In this example, the divide function is initially unprotected. However, the handle_zero_division_error function is specified as the exception handler for ZeroDivisionError at a later time. This allows the exception handlers to be specified at runtime or in a different module.

>>> divide(4, 2) # Returns 2.0 as normal
2.0
>>> divide(4, 0) # Prints "Denominator cannot be zero" and returns None
Denominator cannot be zero

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

safe_callback-0.0.5.tar.gz (3.6 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

safe_callback-0.0.5-py3-none-any.whl (3.9 kB view details)

Uploaded Python 3

File details

Details for the file safe_callback-0.0.5.tar.gz.

File metadata

  • Download URL: safe_callback-0.0.5.tar.gz
  • Upload date:
  • Size: 3.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.0.0 CPython/3.12.2

File hashes

Hashes for safe_callback-0.0.5.tar.gz
Algorithm Hash digest
SHA256 c938b7f3cbc72492c3537f3907c2f78ea223a7d1791dfb3447b43ee288001d0f
MD5 2fc783d35f1882d56fb212bffcf2dea7
BLAKE2b-256 dfbcdd6bdb75447aa6d5b1efc6b19c29c3ac9f515e9134e67255faec48e1fa76

See more details on using hashes here.

File details

Details for the file safe_callback-0.0.5-py3-none-any.whl.

File metadata

  • Download URL: safe_callback-0.0.5-py3-none-any.whl
  • Upload date:
  • Size: 3.9 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.0.0 CPython/3.12.2

File hashes

Hashes for safe_callback-0.0.5-py3-none-any.whl
Algorithm Hash digest
SHA256 69f4513bcf571a1ad7b7ac88d5f3ebafc8224b0e37b587ab9b0b1c9018dec15a
MD5 5250b75a34aeefe451cbd7ae1d4caf5b
BLAKE2b-256 c223057e018ca2509d15fc3c0f91c0765592a9397568b2fe20a66ab521296686

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page