Skip to content
/ grit Public

Context manager for shrugging off and handling exceptions

License

Notifications You must be signed in to change notification settings

Kilo59/grit

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

4 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

grit

ci pypi version Python Versions Code style: black

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 or explicitly set it a None.

>>> from grit import Grit
>>> with Grit(fallback_handler=print):
...     raise TypeError("what have I done?!")
what have I done?!

Usage Examples

TODO ...

About

Context manager for shrugging off and handling exceptions

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages