Intercept your Python exceptions! As awesome as driving a real interceptor!
You can use the intercept
decorator to return a value or raise a specific error when the declared exceptions are raised:
from intercept import intercept, returns, raises
@intercept({
TypeError: returns('intercepted!'),
AttributeError: raises(Exception('intercepted!'))
})
def test(critical: bool):
if not critical:
raise TypeError
else:
raise AttributeError
Also, you might also want to use callables if you need to inspect the raised errors:
from intercept import intercept, returns
@intercept({
TypeError: returns(lambda e: 'intercepted {}'.format(e))
})
def test():
raise TypeError('inner exception')
The decorator can be instantiated:
from intercept import intercept, returns
interceptor = intercept({
TypeError: returns('intercepted!')
})
@interceptor
def test():
raise TypeError
so you can reuse it. Bonus: you can name a variable after a spaceship model.
Tested with Python 3.5.
You can obtain intercept
from PyPI:
pip install intercept