Helpers for handling Python errors
Project description
errorhelpers
Helpers for handling Python errors.
Example 1: Basic usage
import pytest
import errorhelpers
@errorhelpers.expect_errors(ZeroDivisionError)
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, `errorhelpers.UnexpectedError("Unexpected error")`
# will be raised instead.
with pytest.raises(errorhelpers.UnexpectedError, match="Unexpected error"):
sensitive_transaction("a", "b")
Example 2: Default value
import pytest
import errorhelpers
@errorhelpers.expect_errors(
ZeroDivisionError, on_unexpected_error=lambda err_, args_, kwargs_: -1
)
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, -1 will be returned.
assert sensitive_transaction("a", "b") == -1
Example 3: Custom error
import pytest
import errorhelpers
class CustomError(Exception):
@classmethod
def raise_(cls, msg):
def raiser(error, args, kwargs):
print("Hiding error:", error, "with args:", args, "and kwargs: ", kwargs)
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' with args: ('a', 'b') and kwargs: {}
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
errorhelpers-0.1.0.tar.gz
(3.5 kB
view details)
Built Distribution
File details
Details for the file errorhelpers-0.1.0.tar.gz
.
File metadata
- Download URL: errorhelpers-0.1.0.tar.gz
- Upload date:
- Size: 3.5 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/1.1.4 CPython/3.8.8 Linux/5.9.16-1-MANJARO
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 6258952735964fff69a6c1724a8d85a1665ae563daf74a7c7095ae6df72eeede |
|
MD5 | 6a3e6abb3a04cc00db8fe346663de37f |
|
BLAKE2b-256 | 9ca03ff97ab104d1172693087ec4dc56b4ea5a20e520dadc8f7ca7c96aa72567 |
File details
Details for the file errorhelpers-0.1.0-py3-none-any.whl
.
File metadata
- Download URL: errorhelpers-0.1.0-py3-none-any.whl
- Upload date:
- Size: 3.8 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/1.1.4 CPython/3.8.8 Linux/5.9.16-1-MANJARO
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | e4cb27bb41c509100215f5b6c96da33de66f4e011ac2956d366d25c09275bac4 |
|
MD5 | 9b930839782de86966aa816f1377f90a |
|
BLAKE2b-256 | 6160a43e3c2960806062206e1172f0ff18b740c02d31cd0bd99f1c2be0c16b23 |