Skip to content

Instantly share code, notes, and snippets.

@weissjeffm
Created January 31, 2014 14:48
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save weissjeffm/8733424 to your computer and use it in GitHub Desktop.
Save weissjeffm/8733424 to your computer and use it in GitHub Desktop.
error handler
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
@seandst
Copy link

seandst commented Jan 31, 2014

Can you give a concrete example of where you'd use this? I'm failing to grok.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment