# pytrycatch
[](https://badge.fury.io/py/pytrycatch)
[](https://pepy.tech/project/pytrycatch)
[](https://opensource.org/licenses/MIT)
`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](https://pypi.org/project/pytrycatch/) via [pip](https://pypi.org/):
```bash
pip install pytrycatch
```
## Usage
### Basic Example
```py
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:
```sh
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:
```py
@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:
```py
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:
```py
@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:
```py
@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](https://github.com/nuhmanpk/pytrycatch/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](https://github.com/nuhmanpk)
Raw data
{
"_id": null,
"home_page": "https://github.com/nuhmanpk/pytrycatch",
"name": "pytrycatch",
"maintainer": null,
"docs_url": null,
"requires_python": ">=3.9",
"maintainer_email": null,
"keywords": null,
"author": "Nuhman PK",
"author_email": "nuhmanpk7@gmail.com",
"download_url": "https://files.pythonhosted.org/packages/71/f2/87697839f2e5265a8f7f3dbccb1705ab3dabc69258d2e5b8b61ef63bbb76/pytrycatch-0.0.3.tar.gz",
"platform": null,
"description": "# pytrycatch\n\n[](https://badge.fury.io/py/pytrycatch)\n[](https://pepy.tech/project/pytrycatch)\n[](https://opensource.org/licenses/MIT)\n\n`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.\n\n## Features\n\n- **Automatic error logging**: Logs exceptions automatically when an error occurs.\n- **Return safe values**: Allows you to return a safe value (e.g., `None`) when an exception is caught.\n- **Custom error handling**: Allows specifying custom functions to handle errors.\n- **Flexible logging**: Customizable logging levels.\n- **Support for multiple exception types**: You can catch different exception types.\n\n## Installation\n\nYou can install [pytrycatch](https://pypi.org/project/pytrycatch/) via [pip](https://pypi.org/):\n\n```bash\npip install pytrycatch\n```\n\n\n## Usage\n### Basic Example\n\n```py\nfrom pytrycatch import handle_errors\n\n@handle_errors(log=True, default_return=None)\ndef test():\n return a + c # NameError will be raised here\n\ntest()\n```\n### Expected Output:\n\n```sh\nERROR:pytrycatch:Exception in test: name 'a' is not defined\n```\n\n## Handling Specific Exceptions\nYou can specify which exception types you want to handle using the exception_types parameter:\n\n```py\n@handle_errors(exception_types=(ZeroDivisionError, ValueError), default_return=\"Error occurred\")\ndef test():\n return 1 / 0 # ZeroDivisionError will be raised here\n\nresult = test()\nprint(result) # Output: \"Error occurred\"\n```\n\n## Custom Error Handling\nYou can define a custom function to handle exceptions:\n\n\n```py\ndef custom_error_handler(func, exception):\n print(f\"Custom handler for function {func.__name__} caught exception: {exception}\")\n\n@handle_errors(log=True, default_return=None, custom_handler=custom_error_handler)\ndef test():\n return 1 / 0 # ZeroDivisionError will be raised here\n\nresult = test() # This will call custom_error_handler\n```\n\n## Custom Logging Levels\nThe log_level parameter allows you to specify the logging level:\n\n```py\n@handle_errors(log=True, default_return=None, log_level=logging.INFO)\ndef test():\n return 1 / 0 # ZeroDivisionError will be raised here\n\nresult = test() # Logs the error at INFO level instead of ERROR\n```\n\n\n## Multiple Exception Types\nYou can catch multiple exceptions by passing a tuple of exception types to the exception_types parameter:\n\n```py\n@handle_errors(exception_types=(ValueError, ZeroDivisionError), default_return=\"An error occurred\")\ndef test():\n return int(\"not a number\") # ValueError will be raised here\n\nresult = test()\nprint(result) # Output: \"An error occurred\"\n```\n\n## Arguments\n* log (bool): Whether to log the exception (default: True).\n* default_return (any): The value to return when an exception occurs (default: None).\n* exception_types (tuple): A tuple of exception types to catch (default: (Exception,)).\n* log_level (int): The logging level to use (default: logging.ERROR).\n* custom_handler (function): A custom handler function that takes the function and exception as arguments (default: None).\n\n\n## Contributing\n[Fork](https://github.com/nuhmanpk/pytrycatch/fork) the repository.\nCreate a new branch (git checkout -b feature-branch).\nCommit your changes (git commit -am 'Add new feature').\nPush to the branch (git push origin feature-branch).\nOpen a pull request.\n\nMade with \u2764\ufe0f by [Nuhman PK](https://github.com/nuhmanpk)\n\n",
"bugtrack_url": null,
"license": null,
"summary": "Simplified exception handling for Python",
"version": "0.0.3",
"project_urls": {
"Documentation": "https://github.com/nuhmanpk/pytrycatch/blob/main/README.md",
"Funding": "https://github.com/sponsors/nuhmanpk",
"Homepage": "https://github.com/nuhmanpk/pytrycatch",
"Source": "https://github.com/nuhmanpk/pytrycatch/",
"Tracker": "https://github.com/nuhmanpk/pytrycatch/issues"
},
"split_keywords": [],
"urls": [
{
"comment_text": "",
"digests": {
"blake2b_256": "8f7b14da5bd9d233279320a043e2dd16ead301f36c93112d49dba8cfe920e0fb",
"md5": "6634ce60a51c1645cf985eff38556e89",
"sha256": "bda66a44f079317873c5335874d3f563af36b89214c77bd720cbc263949f9b96"
},
"downloads": -1,
"filename": "pytrycatch-0.0.3-py3-none-any.whl",
"has_sig": false,
"md5_digest": "6634ce60a51c1645cf985eff38556e89",
"packagetype": "bdist_wheel",
"python_version": "py3",
"requires_python": ">=3.9",
"size": 5010,
"upload_time": "2024-11-24T12:10:58",
"upload_time_iso_8601": "2024-11-24T12:10:58.564533Z",
"url": "https://files.pythonhosted.org/packages/8f/7b/14da5bd9d233279320a043e2dd16ead301f36c93112d49dba8cfe920e0fb/pytrycatch-0.0.3-py3-none-any.whl",
"yanked": false,
"yanked_reason": null
},
{
"comment_text": "",
"digests": {
"blake2b_256": "71f287697839f2e5265a8f7f3dbccb1705ab3dabc69258d2e5b8b61ef63bbb76",
"md5": "e225ba86d689c756676aae756102abaa",
"sha256": "3d88defed92d7b6f66f1bfba1cad7f258651f6299ac0e84d6e0684b36a69ece0"
},
"downloads": -1,
"filename": "pytrycatch-0.0.3.tar.gz",
"has_sig": false,
"md5_digest": "e225ba86d689c756676aae756102abaa",
"packagetype": "sdist",
"python_version": "source",
"requires_python": ">=3.9",
"size": 4153,
"upload_time": "2024-11-24T12:11:00",
"upload_time_iso_8601": "2024-11-24T12:11:00.109072Z",
"url": "https://files.pythonhosted.org/packages/71/f2/87697839f2e5265a8f7f3dbccb1705ab3dabc69258d2e5b8b61ef63bbb76/pytrycatch-0.0.3.tar.gz",
"yanked": false,
"yanked_reason": null
}
],
"upload_time": "2024-11-24 12:11:00",
"github": true,
"gitlab": false,
"bitbucket": false,
"codeberg": false,
"github_user": "nuhmanpk",
"github_project": "pytrycatch",
"travis_ci": false,
"coveralls": false,
"github_actions": true,
"requirements": [
{
"name": "pytest",
"specs": [
[
">=",
"7.0"
]
]
},
{
"name": "flake8",
"specs": [
[
">=",
"6.0"
]
]
}
],
"lcname": "pytrycatch"
}