Skip to main content

Decorator for catching exceptions in functions and methods.

Project description

exdec

PyPI Total alerts Language grade: Python

Decorator for catching exceptions in functions and methods.

  • Works with both synchronous and asynchronous functions and methods;
  • Catches exceptions of the required types;
  • Three types of handlers are available: before the start of the function, after its end, and the exception handler;
  • Handlers can be both synchronous and asynchronous;
  • All current information about the function is available in any handler;
  • Ability to change the incoming data and the result of the function in the handlers;
  • Several ways to fine-tune and pre-configure the decorator;

Installation

pip install exdec

Quick start

More examples in the examples folder.

from typing import Optional

from exdec.data_classes import FuncInfo
from exdec.decorator import catch


# 1 --------------------------------------------------------------------------

# Catching all exceptions
@catch
def safe_div_1(x: int, y: int) -> Optional[float]:
    result = x / y
    return result


z = safe_div_1(3, 3)
assert z == 1.0
z = safe_div_1(3, 0)
assert z is None


# 2 --------------------------------------------------------------------------

# Catching only ZeroDivisionError
@catch(ZeroDivisionError)
def safe_div_2(x: int, y: int) -> Optional[float]:
    result = x / y
    return result


z = safe_div_2(3, 0)
assert z is None


# 3 --------------------------------------------------------------------------

HANDLER_RESULT = 0.0


def exc_handler(func_info: FuncInfo) -> float:
    exc = func_info.exception
    print(f"Caught an exception {type(exc)}: {exc}.")
    fname = func_info.func.__name__
    args = func_info.args
    print(f"Result {fname}{args} changed to {HANDLER_RESULT}")
    return HANDLER_RESULT


# Catching only ZeroDivisionError
@catch(ZeroDivisionError, exc_handler=exc_handler)
def safe_div_3(x: int, y: int) -> float:
    result = x / y
    return result


z = safe_div_3(3, 0)
assert z == HANDLER_RESULT


# 4 --------------------------------------------------------------------------

class MyException_1(Exception):
    pass


class MyException_2(Exception):
    pass


# Catching all exceptions, except for (MyException_1, MyException_2)
@catch(MyException_1, MyException_2, exclude=True, exc_handler=exc_handler)
def safe_div_4(x: int, y: int) -> float:
    result = x / y
    return result


z = safe_div_4(3, 0)
assert z == HANDLER_RESULT


# 5 --------------------------------------------------------------------------

def exc_handler_reraise(func_info: FuncInfo) -> float:
    exc = func_info.exception
    print(f">>> Caught an exception {type(exc)}: {exc}. \nRERAISE!")
    raise exc


# Catching only (MyException_1, ZeroDivisionError) and reraise
@catch(MyException_1, ZeroDivisionError, exc_handler=exc_handler_reraise)
def div(x: int, y: int) -> float:
    result = x / y
    return result


div(3, 0)  # Exception

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

exdec-0.3.2.tar.gz (6.3 kB view hashes)

Uploaded Source

Built Distribution

exdec-0.3.2-py3-none-any.whl (6.9 kB view hashes)

Uploaded Python 3

Supported by

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