Created
January 31, 2014 14:48
-
-
Save weissjeffm/8733424 to your computer and use it in GitHub Desktop.
error handler
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from contextlib import contextmanager | |
import re | |
from functools import partial | |
@contextmanager | |
def handler(f): | |
try: | |
yield | |
except Exception as e: | |
if not f(e): | |
raise e | |
def regex(expr, e): | |
p = re.compile(expr) | |
return p.search(e.message) | |
with handler(partial(regex, 'foo')): | |
x = 1 | |
raise Exception('oh noes foo happened!') # this will be caught because regex matches |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Can you give a concrete example of where you'd use this? I'm failing to grok.