Skip to content

ikanor/intercept

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

5 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

intercept

Intercept your Python exceptions! As awesome as driving a real interceptor!

As awesome as this.

Usage

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.

Compatibility

Tested with Python 3.5.

Installation

You can obtain intercept from PyPI:

pip install intercept

About

Gracefully catch Python exceptions with a decorator

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Languages