Skip to main content

Helpers for handling Python errors

Project description

errorhelpers

PyPI version PyPI pyversions Maintainability Test Coverage

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


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.1.tar.gz (3.5 kB view details)

Uploaded Source

Built Distribution

errorhelpers-0.1.1-py3-none-any.whl (3.8 kB view details)

Uploaded Python 3

File details

Details for the file errorhelpers-0.1.1.tar.gz.

File metadata

  • Download URL: errorhelpers-0.1.1.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

Hashes for errorhelpers-0.1.1.tar.gz
Algorithm Hash digest
SHA256 7388358f93ef57003b652046bd3827c15d077659bd4b62cb65e6b3659f3b71a6
MD5 db22e785d4c229779e967cc80cf3c297
BLAKE2b-256 558031704d3c795e067effd450f5462d375ead109da5d9864a1d226b55fda02c

See more details on using hashes here.

File details

Details for the file errorhelpers-0.1.1-py3-none-any.whl.

File metadata

  • Download URL: errorhelpers-0.1.1-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

Hashes for errorhelpers-0.1.1-py3-none-any.whl
Algorithm Hash digest
SHA256 11c063ab2c5cb9ff248f1ec0b73fe4310e39177d9e8b2a275937325d9d598457
MD5 7cf08e8d527834b3ef0ad793591ed767
BLAKE2b-256 84cf6ef151450f07766964c15cb4910578eb19b0f4f287e7686ee7f005943708

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page