https://github.com/sayanarijit/errorhelpers
Helpers for handling Python errors.
https://github.com/sayanarijit/errorhelpers
Last synced: 9 days ago
JSON representation
Helpers for handling Python errors.
- Host: GitHub
- URL: https://github.com/sayanarijit/errorhelpers
- Owner: sayanarijit
- License: mit
- Created: 2021-03-22T06:37:30.000Z (over 4 years ago)
- Default Branch: main
- Last Pushed: 2021-03-22T08:28:07.000Z (over 4 years ago)
- Last Synced: 2025-09-25T08:19:55.658Z (17 days ago)
- Language: Python
- Homepage:
- Size: 15.6 KB
- Stars: 0
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
errorhelpers
============[](https://pypi.org/project/errorhelpers)
[](https://pypi.org/project/errorhelpers)
[](https://codeclimate.com/github/sayanarijit/errorhelpers/maintainability)
[](https://codeclimate.com/github/sayanarijit/errorhelpers/test_coverage)Helpers for handling Python errors.
### Usage:
```python
# As a decorator
@errorhelpers.expect_error(*errors, on_unexpected_error=handler)
def some_error_prone_funcion():
...# Using with statement
with errorhelpers.expect_error(*errors, on_unexpected_error=handler):
# Some error prone operation
...
```### Example 1: Basic usage
```python
import pytest
import errorhelperswith errorhelpers.expect_errors(ZeroDivisionError):
assert 4 / 2 == 2# `ZeroDivisionError` will be re-raised.
with pytest.raises(ZeroDivisionError):
with errorhelpers.expect_errors(ZeroDivisionError):
4 / 0# In case of other exceptions, `errorhelpers.UnexpectedError("Unexpected error")`
# will be raised instead.
with pytest.raises(errorhelpers.UnexpectedError, match="Unexpected error"):
with errorhelpers.expect_errors(ZeroDivisionError):
"a" / "b"
```### Example 2: Custom error
```python
import pytest
import errorhelpersclass CustomError(Exception):
@classmethod
def raise_(cls, msg):
def raiser(error):
print("Hiding error:", error)
raise cls(msg)return raiser
@errorhelpers.expect_errors(
ZeroDivisionError, on_unexpected_error=CustomError.raise_("Custom error")
)
def sensitive_transaction(x, y):
return int(x) / int(y)assert sensitive_transaction(4, "2") == 2
# `ZeroDivisionError` will be re-raised.
with pytest.raises(ZeroDivisionError):
sensitive_transaction(4, 0)# In case of other exceptions, `CustomError` will be raised instead.
with pytest.raises(CustomError, match="Custom error"):
sensitive_transaction("a", "b")# Hiding error: invalid literal for int() with base 10: 'a'
```