Created
March 29, 2011 02:41
-
-
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.
This file contains hidden or 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 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