Skip to content

Instantly share code, notes, and snippets.

@jhorman
Created March 29, 2011 02:41
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 jhorman/891719 to your computer and use it in GitHub Desktop.
Save jhorman/891719 to your computer and use it in GitHub Desktop.
Decorator that throws traps a twisted error and rethrows your app specific error.
def wrap_exception(handle, error_type, error_message, log=None):
def attach_check(method):
@functools.wraps(method)
def wrapper(self, *args, **kwargs):
d = method(self, *args, **kwargs)
def on_error(failure):
failure.trap(handle)
logger = logging if not log else logging.getLogger(log)
logger.error("%s, %s" % (error_message, failure.getErrorMessage()))
raise error_type(error_message)
d.addErrback(on_error)
return d
return wrapper
return attach_check
# Example
@wrap_exception(ConnectError, ExternalStorageException, 'error indexing block in elastic', log='SbrDao')
def elastic_index(self, block):
pass
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment