Created
February 9, 2012 20:08
-
-
Save clayg/1782740 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
class ResourceExceptionHandler(object): | |
"""Context manager to handle Resource exceptions. | |
Used when processing exceptions generated by API implementation | |
methods (or their extensions). Converts most exceptions to Fault | |
exceptions, with the appropriate logging. | |
""" | |
def __enter__(self): | |
return None | |
def __exit__(self, ex_type, ex_value, ex_traceback): | |
if not ex_value: | |
return True | |
if isinstance(ex_value, exception.NotAuthorized): | |
msg = unicode(ex_value) | |
raise Fault(webob.exc.HTTPForbidden(explanation=msg)) | |
elif isinstance(ex_value, TypeError): | |
LOG.exception(ex_value) | |
raise Fault(webob.exc.HTTPBadRequest()) | |
elif isinstance(ex_value, Fault): | |
LOG.info(_("Fault thrown: %s"), unicode(ex_value)) | |
raise ex_value | |
elif isinstance(ex_value, webob.exc.HTTPException): | |
LOG.info(_("HTTP exception thrown: %s"), unicode(ex_value)) | |
raise Fault(ex_value) | |
# We didn't handle the exception | |
return False |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment