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

Uploaded Source

Built Distribution

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

Uploaded Python 3

File details

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

File metadata

  • Download URL: la-catch-0.0.2.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.2.tar.gz
Algorithm Hash digest
SHA256 b8790599416bee93cf5d2e24e82164ef9968f17c36d375d9b41898d6974cfbbd
MD5 7869c5e3cd12a4c62c36022565d45194
BLAKE2b-256 346c1f4bc2db1f253335af4fc99ac7f25d915da6e174b08ef7522b75c3d6e6e4

See more details on using hashes here.

File details

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

File metadata

  • Download URL: la_catch-0.0.2-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.2-py3-none-any.whl
Algorithm Hash digest
SHA256 dff3266ac0859285449306deab909b7e8d24c228f942da75e510fa46c7ed5aab
MD5 2f7b3f37e22c292b462bbf06ed4f7688
BLAKE2b-256 c1720a19815d897d5ca51b96949caa55cbbea653c36949d744967b93d01e9f0c

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