Skip to main content

pytrycatch

PyPI version Downloads License

pytrycatch is a Python package that simplifies exception handling by providing a decorator to catch exceptions, log them, and return default values. It offers easy customization, including custom error handling functions, logging levels, and exception types.

Features

  • Automatic error logging: Logs exceptions automatically when an error occurs.
  • Return safe values: Allows you to return a safe value (e.g., None) when an exception is caught.
  • Custom error handling: Allows specifying custom functions to handle errors.
  • Flexible logging: Customizable logging levels.
  • Support for multiple exception types: You can catch different exception types.

Installation

You can install pytrycatch via pip:

pip install pytrycatch

Usage

Basic Example

from pytrycatch import handle_errors

@handle_errors(log=True, default_return=None)
def test():
    return a + c  # NameError will be raised here

test()

Expected Output:

ERROR:pytrycatch:Exception in test: name 'a' is not defined

Handling Specific Exceptions

You can specify which exception types you want to handle using the exception_types parameter:

@handle_errors(exception_types=(ZeroDivisionError, ValueError), default_return="Error occurred")
def test():
    return 1 / 0  # ZeroDivisionError will be raised here

result = test()
print(result)  # Output: "Error occurred"

Custom Error Handling

You can define a custom function to handle exceptions:

def custom_error_handler(func, exception):
    print(f"Custom handler for function {func.__name__} caught exception: {exception}")

@handle_errors(log=True, default_return=None, custom_handler=custom_error_handler)
def test():
    return 1 / 0  # ZeroDivisionError will be raised here

result = test()  # This will call custom_error_handler

Custom Logging Levels

The log_level parameter allows you to specify the logging level:

@handle_errors(log=True, default_return=None, log_level=logging.INFO)
def test():
    return 1 / 0  # ZeroDivisionError will be raised here

result = test()  # Logs the error at INFO level instead of ERROR

Multiple Exception Types

You can catch multiple exceptions by passing a tuple of exception types to the exception_types parameter:

@handle_errors(exception_types=(ValueError, ZeroDivisionError), default_return="An error occurred")
def test():
    return int("not a number")  # ValueError will be raised here

result = test()
print(result)  # Output: "An error occurred"

Arguments

  • log (bool): Whether to log the exception (default: True).
  • default_return (any): The value to return when an exception occurs (default: None).
  • exception_types (tuple): A tuple of exception types to catch (default: (Exception,)).
  • log_level (int): The logging level to use (default: logging.ERROR).
  • custom_handler (function): A custom handler function that takes the function and exception as arguments (default: None).

Contributing

Fork the repository. Create a new branch (git checkout -b feature-branch). Commit your changes (git commit -am 'Add new feature'). Push to the branch (git push origin feature-branch). Open a pull request.

Made with ❤️ by Nuhman PK

Metadata

Release files for pytrycatch 0.0.3

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

Source distribution (sdist)

Source distribution for pytrycatch 0.0.3
File Size Uploaded Provenance Actions
pytrycatch-0.0.3.tar.gz 4.2 kB Without provenance Details for pytrycatch-0.0.3.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for pytrycatch 0.0.3
File Interpreter ABI Platform Actions
pytrycatch-0.0.3-py3-none-any.whl Python 3 none any Details for pytrycatch-0.0.3-py3-none-any.whl

Total release size: 9.2 kB

Release files / pytrycatch-0.0.3.tar.gz

Download URL pytrycatch-0.0.3.tar.gz
Size 4.2 kB
Tags Source
SHA-256 checksum
How to use checksums
3d88defed92d7b6f66f1bfba1cad7f258651f6299ac0e84d6e0684b36a69ece0
BLAKE2b-256 checksum
How to use checksums
71f287697839f2e5265a8f7f3dbccb1705ab3dabc69258d2e5b8b61ef63bbb76
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/5.1.1 CPython/3.9.20

Release files / pytrycatch-0.0.3-py3-none-any.whl

Download URL pytrycatch-0.0.3-py3-none-any.whl
Size 5.0 kB
Tags Python 3
SHA-256 checksum
How to use checksums
bda66a44f079317873c5335874d3f563af36b89214c77bd720cbc263949f9b96
BLAKE2b-256 checksum
How to use checksums
8f7b14da5bd9d233279320a043e2dd16ead301f36c93112d49dba8cfe920e0fb
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/5.1.1 CPython/3.9.20

Security for pytrycatch 0.0.3

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