Simplified exception handling for Python
Project description
pytrycatch
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
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file pytrycatch-0.0.3.tar.gz.
File metadata
- Download URL: pytrycatch-0.0.3.tar.gz
- Upload date:
- Size: 4.2 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/5.1.1 CPython/3.9.20
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
3d88defed92d7b6f66f1bfba1cad7f258651f6299ac0e84d6e0684b36a69ece0
|
|
| MD5 |
e225ba86d689c756676aae756102abaa
|
|
| BLAKE2b-256 |
71f287697839f2e5265a8f7f3dbccb1705ab3dabc69258d2e5b8b61ef63bbb76
|
File details
Details for the file pytrycatch-0.0.3-py3-none-any.whl.
File metadata
- Download URL: pytrycatch-0.0.3-py3-none-any.whl
- Upload date:
- Size: 5.0 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/5.1.1 CPython/3.9.20
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
bda66a44f079317873c5335874d3f563af36b89214c77bd720cbc263949f9b96
|
|
| MD5 |
6634ce60a51c1645cf985eff38556e89
|
|
| BLAKE2b-256 |
8f7b14da5bd9d233279320a043e2dd16ead301f36c93112d49dba8cfe920e0fb
|