Skip to main content

la-catch

Decorator and context manager to catch exception(s) and call a function to handle the error.

install

pip install la-catch

syntax

Catch(exceptions, callback, *args, **kwargs)

exceptions - Exception or Tuple of exceptions to be captured
callback - function to be called in case of exceptions being raised or a return value to be returned in case of exceptions
*args - Any extra arguments are passed to context manager callback
**kwargs - Any extra keywords are passed to context manager callback

usage

There is two ways to use catch:

  • Context Manager
  • Decorator

context manager

Catch exception and do nothing:

from la_catch import Catch

def func():
    with Catch(TypeError):
        raise TypeError("example")

Catch exception and call callback passing exception:

from la_catch import Catch

def on_error(exception):
    print(exception)

def func():
    with Catch(TypeError, callback=on_error):
        raise TypeError("example")

Catch exception and call callback passing keywords arguments and exception:

from la_catch import Catch

def on_error(message, exception):
    print(message, exception)

def func():
    with Catch(TypeError, callback=on_error, message="WARNING:"):
        raise TypeError("example")

Catch multiple exceptions:

from la_catch import Catch

def func():
    with Catch((TypeError, FileNotFoundError)):
        raise FileNotFoundError("example")

decorator

Make sure that the callback signature identical to the function signature.

Catch exception and return None:

from la_catch import Catch

@Catch(TypeError)
def func():
    raise TypeError("example")

Catch exception and return a value:

from la_catch import Catch

@Catch(TypeError, callback=10)
def func():
    raise TypeError("example")

Catch exception and call callback passing exception:

from la_catch import Catch

def on_error(exception):
    print(exception)

@Catch(TypeError, callback=on_error)
def func():
    raise TypeError("example")

Catch exception and call callback passing decorated function arguments and exception:

from la_catch import Catch

def on_error(message, exception):
    print(message, exception)

@Catch(TypeError, callback=on_error)
def func(message="WARNING:"):
    raise TypeError("example")

Chain catchs:

from la_catch import Catch

def on_file_not_found_error(exception):
    print(exception)

def on_typerror(exception):
    print(exception)

@Catch(FileNotFoundError, callback=on_file_not_found_error)
@Catch(TypeError, callback=on_typerror)
def func():
    raise FileNotFoundError("example")
    raise TypeError("example")

notes

Initialization arguments are used by context manager and ignored by decorators, because:

  • Would have to decide a priority between intialization arguments and function arguments
  • Passing default values to callback would need me to know the function arguments
    • When chaining decorators, wasn't possible to discover the function parameters because decorators would give (*args, **kwargs)
    • When chaining decorators, wasn't possible to discover the default values because decorators would give (*args, **kwargs)

Metadata

Release files for la-catch 0.0.5

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Built distribution (wheel)

Table of built distributions (wheels) for la-catch 0.0.5
File Interpreter ABI Platform Actions
la_catch-0.0.5-py3-none-any.whl Python 3 none any Details for la_catch-0.0.5-py3-none-any.whl

Release files / la_catch-0.0.5-py3-none-any.whl

Download URL la_catch-0.0.5-py3-none-any.whl
Size 4.0 kB
Tags Python 3
SHA-256 checksum
How to use checksums
3faac571e1bcb773cad1c455ed1b7009738c4db8692fa17cdf0305b74bb04dd7
BLAKE2b-256 checksum
How to use checksums
fab40497ff6b6ec155a9c4d61a7be66735d22b657157e224534992e359eebab9
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.1 CPython/3.8.10

Security for la-catch 0.0.5

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page