Exception handling context manager.
Project description
grit
Context manager for shrugging off exceptions less badly.
Subtitle: exception handling for lazy people.
Description and Rationale
Does your code suffer from an overuse of bare exceptions?
What if you could still shrug off exceptions when needed but not be so cringe about it?
try:
foobar()
except Exception:
pass
from grit import Grit
# Full exception stacktrace is automatically logged
with Grit():
foobar()
Quick start
pip install py-grit
>>> from grit import Grit
>>> with Grit():
... raise RunTimeError("something bad")
>>> print("Uh, everything is under control. Situation normal")
Uh, everything is under control. Situation normal
Propagate the errors you care about, while ignoring the ones you don't.
>>> from grit import Grit
>>> with Grit(dnr_list=[ZeroDivisionError]):
... 42 / 0
Traceback (most recent call last):
...
ZeroDivisionError: division by zero
And handle those that require special attention
>>> from grit import Grit
>>> with Grit(handlers={ValueError: print}):
... raise ValueError("what have you done?!")
what have you done?!
Logging and results
Grit()
accepts a fallback_handler
callable which will be called on the exception if no specific
'handler' (handlers
) is found.
By default the fallback_handler
will log a full exception traceback at the debug level using self.logger
.
To change this behavior, provide your own fallback_handler
.
>>> from grit import Grit
>>> with Grit(fallback_handler=print):
... raise TypeError("what have I done?!")
what have I done?!
Usage Examples
TODO ...
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
Built Distribution
Hashes for py_grit-0.0.1a2-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | aba1c7d6395a96a2d4c48f2b679af8746cf44bbf37f2bceeffcf061a23b10cb1 |
|
MD5 | fbdebc26249543acdcd7a3a739fe8f93 |
|
BLAKE2b-256 | eaa0bbc134fe34a8f94cf7c8e07a99a8c94374f05514a6683f1bcde5b85814f4 |