def wrap_exception(notifier=None, event_type=None):
"""This decorator wraps a method to catch any exceptions.
It logs the exception as well as optionally sending
it to the notification system.
"""
def inner(f):
def wrapped(self, context, *args, **kwargs):
# Don't store self or context in the payload, it now seems to
# contain confidential information.
try:
return f(self, context, *args, **kwargs)
except Exception as e:
with excutils.save_and_reraise_exception():
if notifier:
call_dict = inspect.getcallargs(f, self, context,
*args, **kwargs)
payload = dict(exception=e,
private=dict(args=call_dict)
)
temp_type = event_type
if not temp_type:
# If f has multiple decorators, they must use
# functools.wraps to ensure the name is
# propagated.
temp_type = f.__name__
notifier.error(context, temp_type, payload)
return functools.wraps(f)(wrapped)
return inner
评论列表
文章目录