"""Utilities related to exception handling.""" import functools from sentry_sdk import capture_exception from src.connectors import logging def sentry_capture_exception(f): """Handle possible exception with Sentry. A decorator to capture and send any Exception instance to Sentry and re-raise it. """ @functools.wraps(f) def wrapper(*args, **kwargs): try: return f(*args, **kwargs) except Exception as e: logging.logger.exception( 'Failed to execute %s() with args: %s, kwargs: %s.', f.__name__, args, kwargs) capture_exception(e) # won't throw error. # Re-raising the error to restart Lambda raise e return wrapper