Skip to main content

Decorator to map exception to functions

Project description

la-catch

Decorator to catch exception(s) and call function to handle the error. Can also define a return value to the function after catching.

install

pip install la-catch

syntax

@catch(exceptions, call, ret)

exceptions - Exception or Tuple of exceptions to be captured
call - function to be called in case of exceptions being raised
ret - value to be returned by the main function in case of exceptions being raised

call receives the same arguments from the decorated function but it appends the exception raised to the list of arguments (*args).
In case an exception is raised and no ret was defined, the return will be the same that call would return.

usages

Let's say that you just expect a function to try running something and if fails ignore and continue the program logic. In this case you just want to catch and ignore the exception:

from la_catch import catch


@catch(Exception)
def example():
    raise Exception("What a great day to raise an exception")

Most of times you want at least to print the exception, in this case you can pass a function to be called. This function will receive the exception as the last argument for you use as you like.

from la_catch import catch


def func(e):
    print("Look what i catched mommy:", e)


@catch(Exception, func)
def example():
    raise Exception("You will never catch me alive")

If all that you want is print the exception/traceback, i would recommend you passing logging.exception as call.

from la_catch import catch


@catch(Exception, logging.exception)
def example():
    raise Exception("I love to read tracebacks")

Let's say that you know how to deal with the raised problem, now you want the function to return the solution of the problem. You can make call return the expected resolution.

from la_catch import catch


def func(e):
    if isinstance(e, ZeroDivisionError):
        print("You can't divide by zero you dummy")
        return 0
    return 1


@catch(Exception, func)
def example():
    return 0/0

Okay okay, but now you want to log the exception and return something so you program doesn't crash. Better than creating a function just for this two things is passing to ret the value to return in case of this exception.

from la_catch import catch


@catch(Exception, logging.exception, 0)
def example():
    return 0/0

Of course that i am using Exception everywhere but you could use other exception or multiple exceptions!

from la_catch import catch


@catch((ZeroDivisionError, OverflowError, FloatingPointError), logging.exception, 0)
def example():
    return 0/0

If you want to make different things to different exceptions just decorate with one more catch.

from la_catch import catch


@catch(OverflowError, logging.exception, None)
@catch(ZeroDivisionError, logging.exception, 0)
def example():
    return 0/0

Just remember that exception is always pass as the last argument from call.

from la_catch import catch


def func(a, b, e):
    print("I have zero tolerance for this error... got it?")


@catch(ZeroDivisionError, func, 0)
def example(a, b):
    return a/b

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

la-catch-0.0.1.tar.gz (3.3 kB view details)

Uploaded Source

Built Distribution

la_catch-0.0.1-py3-none-any.whl (3.5 kB view details)

Uploaded Python 3

File details

Details for the file la-catch-0.0.1.tar.gz.

File metadata

  • Download URL: la-catch-0.0.1.tar.gz
  • Upload date:
  • Size: 3.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.7.1 importlib_metadata/4.10.1 pkginfo/1.8.2 requests/2.27.1 requests-toolbelt/0.9.1 tqdm/4.62.3 CPython/3.10.1

File hashes

Hashes for la-catch-0.0.1.tar.gz
Algorithm Hash digest
SHA256 14cc702dd2f2c139d16a46d6c65bf78af244d81ee7e677cb6daad63a764cab85
MD5 7e6ed7cb12d62052aa0bbf1ab1f586bd
BLAKE2b-256 969b9bcc73fd7ebb06a532b6b991ef854a04326e475d23ab320b3f2bc02f1d34

See more details on using hashes here.

File details

Details for the file la_catch-0.0.1-py3-none-any.whl.

File metadata

  • Download URL: la_catch-0.0.1-py3-none-any.whl
  • Upload date:
  • Size: 3.5 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.7.1 importlib_metadata/4.10.1 pkginfo/1.8.2 requests/2.27.1 requests-toolbelt/0.9.1 tqdm/4.62.3 CPython/3.10.1

File hashes

Hashes for la_catch-0.0.1-py3-none-any.whl
Algorithm Hash digest
SHA256 841917eef97ab4381ae4f3f9012e865db43d99e43bb38005c2c398e47428c6ac
MD5 59eb20dc98da76db47285a8111cc4e8e
BLAKE2b-256 8542acc3aa50296063590b6a2447a285c0b645a0b355bef1038e068515aaf892

See more details on using hashes here.

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