Created
January 27, 2011 21:01
-
-
Save carljm/799264 to your computer and use it in GitHub Desktop.
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
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