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]:
    return x / y


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


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

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


assert safe_div_2(3, 0) is None


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

HANDLER_RESULT = 0.0


def exc_handler(func_info: FuncInfo) -> float:
    msg = f"Caught an exception, func_info={func_info}."
    print(f"{msg} Result 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:
    return x / y


assert safe_div_3(3, 0) == 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:
    return x / y


assert safe_div_4(3, 0) == HANDLER_RESULT


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

# For methods everything works the same
class MathFunctions:

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


math_functions = MathFunctions()
assert math_functions.safe_div_5(3, 0) is None


# 6 --------------------------------------------------------------------------

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


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


div_6(3, 0)  # ZeroDivisionError

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.4.1.tar.gz (5.3 kB view details)

Uploaded Source

Built Distribution

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

exdec-0.4.1-py3-none-any.whl (6.0 kB view details)

Uploaded Python 3

File details

Details for the file exdec-0.4.1.tar.gz.

File metadata

  • Download URL: exdec-0.4.1.tar.gz
  • Upload date:
  • Size: 5.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.4.2 importlib_metadata/4.8.1 pkginfo/1.7.1 requests/2.26.0 requests-toolbelt/0.9.1 tqdm/4.62.3 CPython/3.9.5

File hashes

Hashes for exdec-0.4.1.tar.gz
Algorithm Hash digest
SHA256 442339698cf5070838a66dd0c4641b0394c1ac4017cf280276f675058b7a619e
MD5 1ce004a4bac56ade0758a09416cd4ef2
BLAKE2b-256 eb5cad1047b172ba1a5c2c1a3f54faec5023a36f8a7284a231c7302c59cba4b2

See more details on using hashes here.

File details

Details for the file exdec-0.4.1-py3-none-any.whl.

File metadata

  • Download URL: exdec-0.4.1-py3-none-any.whl
  • Upload date:
  • Size: 6.0 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.4.2 importlib_metadata/4.8.1 pkginfo/1.7.1 requests/2.26.0 requests-toolbelt/0.9.1 tqdm/4.62.3 CPython/3.9.5

File hashes

Hashes for exdec-0.4.1-py3-none-any.whl
Algorithm Hash digest
SHA256 39be519c04c7ad21538339bdf88f5f7afea721b41b10bc185af4a1107136a037
MD5 b9709be02819451dfa329ffa03913174
BLAKE2b-256 a5dc3adbf35fef82092c030792d488604a0bea8a509a175184de688ecda15ec5

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