"""Sentry Client. Provides sentry_client attribute for ad hoc capturing of messages and errors. """ import traceback from contextlib import contextmanager from sys import exit as sysexit from functools import wraps, partial import config @contextmanager def sentry_client(): """Yields a correctly configured sentry_sdk context.""" import sentry_sdk sentry_sdk.init( config.SENTRY, traces_sample_rate=config.SENTRY_TRACE_LEVEL, environment=config.ENVIRONMENT ) yield sentry_sdk def sentry_wrap(function, terminal=False): """Sentry Wrapper. Wraps a method in a sentry try/catch. """ @wraps(function) def wrapper(*args, **kwargs): # session = kwargs.pop('session', None) # if session: try: return function(*args, **kwargs) except Exception: if config.SENTRY: with sentry_client() as sent_client: sent_client.capture_exception() if terminal: print(traceback.format_exc()) sysexit() raise return wrapper sentry_wrap_terminal = partial(sentry_wrap, terminal=True)