Skip to main content

Elegant detailed Python exceptions.

Project description

errr

Elegantly create detailed exceptions in Python.

Detailed exceptions

>>> import errr
>>> class MyException(errr.DetailedException, list_detailts=True, details=["cause", "type"]):
...  pass
...
>>> example = MyException("The backend server crashed", "backend", "crash")
>>> raise example
__main__.MyException: The backend server crashed

Details:
 ˪cause: backend
 ˪type: crash
>>> example.details
{'cause': 'backend', 'type': 'crash'}
>>> example.cause
'backend'

Semantic exceptions

You can also rapidly create large semantic trees of exceptions using the make_tree function, listing exceptions as keyword arguments using the errr.exception factory method. The make_tree method executes these recursive factories to produce your exceptions. Nesting these factory methods will make the resultant exceptions inherit from eachother. All of the produced exceptions are then flat injected into the given module dictionary (typically) this should be globals() but you can inject into other modules using sys.modules["name"].__dict__.

from errr import make_tree, exception as _e

make_tree(
  # Pass the module dictionary as first argument
  globals(),
  # List your nested exceptions
  RootException=_e(
    ChildException=_e(),
    Child2Exception=_e()
  ),
  SecondRootException=_e(
    # List details as positional arguments
    "detail1", "detail2",
    # And continue with child exceptions as keyword arguments
    AnotherChildException=_e()
  )
)

print(RootException)
# <class '__main__.RootException'>
print(ChildException)
# <class '__main__.ChildException'>
print(ChildException.__bases__)
# (<class '__main__.RootException'>,)

Exception wrapping

You can catch and reraise exceptions as a new type of exception with the wrap function:

import errr

class LibraryError(errr.DetailedException, details=["library"]):
  pass

for name, library in libraries.items():
  try:
    library.load()
  except Exception as e:
    errr.wrap(LibraryError, e, name, prepend="When trying to load %library% it reported:\n")
# Traceback
#   ...
# __main__.LibraryError: When trying to load myLibrary it reported:
# Module 'missing' not found.

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

errr-1.2.0.tar.gz (5.4 kB view details)

Uploaded Source

Built Distribution

errr-1.2.0-py3-none-any.whl (5.9 kB view details)

Uploaded Python 3

File details

Details for the file errr-1.2.0.tar.gz.

File metadata

  • Download URL: errr-1.2.0.tar.gz
  • Upload date:
  • Size: 5.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.1 CPython/3.9.10

File hashes

Hashes for errr-1.2.0.tar.gz
Algorithm Hash digest
SHA256 2824b83edbd1f61d354c60e36400b2e31ab83e2094acb837b86686892b866e76
MD5 a23d23ab2662db6365e86d9dc8c18591
BLAKE2b-256 3160d9685a931dd93f89d4372b789a7bb10ba95b4d83d177b874ba0914552562

See more details on using hashes here.

File details

Details for the file errr-1.2.0-py3-none-any.whl.

File metadata

  • Download URL: errr-1.2.0-py3-none-any.whl
  • Upload date:
  • Size: 5.9 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.1 CPython/3.9.10

File hashes

Hashes for errr-1.2.0-py3-none-any.whl
Algorithm Hash digest
SHA256 adc785eef4d6cb2072f19bdcb051f4b818bf6ce1036a606b1917284b45ca8807
MD5 ce0dbf9abc081cf9595a1845f85b3fa6
BLAKE2b-256 c84410aac23018bd13b52b0cff7d6d44079c117a808322448ffd4e0d05546835

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