""" Sentry Client ============= Provides sentry_client attribute for ad hoc capturing of messages and errors. https://docs.getsentry.com/hosted/clients/python/usage/ """ import sentry_sdk from ytownership import config def before_send_exception_to_sentry(event, hint): """Filter exceptions sent to sentry. :param event: Sentry event containing exception context. :param hint: Sentry hint containing raised exception. :return: event or None """ if 'exc_info' not in hint: return event # Ignore 404 and 500 errors from YT API matches = ['HttpError 404', 'HttpError 500'] _ignore_0, exc_value, _ignore_1 = hint['exc_info'] if any(x in exc_value for x in matches): return None return event sentry_client = None if config.SENTRY_DSN: sentry_client = sentry_sdk.init( config.SENTRY_DSN, before_send=before_send_exception_to_sentry)