Skip to content

Instantly share code, notes, and snippets.

@carljm
Created January 27, 2011 21:01
Show Gist options
  • Save carljm/799264 to your computer and use it in GitHub Desktop.
Save carljm/799264 to your computer and use it in GitHub Desktop.
def unwrap_template_syntax_error(function, *unwrap_exceptions):
"""
A decorator to catch TemplateSyntaxError and unwrap it, reraising the
wrapped exception.
If unwrap_exceptions are passed, the unwrapping will only occur if the
wrapped exception is one of those exception types.
"""
@wraps(function)
def _wrapped(*args, **kwargs):
try:
return function(*args, **kwargs)
except TemplateSyntaxError, e:
wrapped = e.exc_info[1]
if (not unwrap_exceptions or
wrapped in unwrap_exceptions or
isinstance(wrapped, unwrap_exceptions)):
raise wrapped
raise
return _wrapped
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment