Skip to main content

Decorators for better error handling

Project description

Erich

enrich local errors without repeated try except using decorators

Example

main.py

from typing import Callable

import erich

# Print a formatted message where format field names must 
# be present in the function signature.
@erich.fmt("Tried starting task {name} ({desc})")
def start_task(name: str, prio: int, fn: Callable, desc: str = None):
    schedule(prio, fn)

# Print that this function has been called but only include the prio
# in the output
@erich.signature("prio")
def schedule(prio: int, fn: Callable):
    can_schedule(prio)
    fn()

# add some nicer output to the final result
@erich.fmt("Cannot schedule task due to")
def can_schedule(prio: int):
    can_schedule_internal(prio)

# here the exception is actually raised with a limited
# amount of information which will be enriched by
# parent/calling functions.
def can_schedule_internal(prio: int):
    raise Exception(f"Cannot schedule something with prio {prio}. It's invalid")

start_task("test", -1, lambda: None, desc="very important")
> python main.py

..
stack trace
..

Tried starting task test (very important) during call of schedule(prio = -1)
  Cannot schedule task due to
   Cannot schedule something with prio -1. It's invalid

Release

  1. update version in setup.cfg, pyproject.toml and root __init__.py
  2. build
python -m build
  1. upload
python3 -m twine upload --repository pypi dist/*

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

erich-0.0.2.tar.gz (4.3 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

erich-0.0.2-py3-none-any.whl (4.7 kB view details)

Uploaded Python 3

File details

Details for the file erich-0.0.2.tar.gz.

File metadata

  • Download URL: erich-0.0.2.tar.gz
  • Upload date:
  • Size: 4.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.10.10

File hashes

Hashes for erich-0.0.2.tar.gz
Algorithm Hash digest
SHA256 d29f0e098e55e7336a5a95688dc46cabcb297f9d2ef16f008af084294dfde8d3
MD5 de47a0b78aab895788483bb2ee7a7743
BLAKE2b-256 c2d4c160ddf93954fc3b927279b308ddb551210258e9bcc86eca367b42a4ae5d

See more details on using hashes here.

File details

Details for the file erich-0.0.2-py3-none-any.whl.

File metadata

  • Download URL: erich-0.0.2-py3-none-any.whl
  • Upload date:
  • Size: 4.7 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.10.10

File hashes

Hashes for erich-0.0.2-py3-none-any.whl
Algorithm Hash digest
SHA256 3dc1e4828610db1ba5abef7660fa9c1403c215439524baa873834f954bd5063f
MD5 c2c81c1688f3377fdfa83bd780b53931
BLAKE2b-256 c18e3db0b2ff5c54c7dce93c1c9b0210f20cab5e3b3b83e1038c91c5a82ba250

See more details on using hashes here.

Supported by

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