"""Sentry Client. Provides functions for capturing messages and errors via Sentry SDK. https://docs.sentry.io/platforms/python/ """ import sentry_sdk from reporting import config def init_sentry(): """Initialise the Sentry SDK. If config.SENTRY is set, events are reported to Sentry. If config.SENTRY is None, the SDK is initialised in a no-op state. """ sentry_sdk.init(dsn=config.SENTRY) def send_response_to_sentry(response, sentry_message): """Send a Response object to Sentry. This method should be used to send a Response object & message to Sentry when an error occurs that we should be alerted about. ex. error_response = response.create_fatal_response('Something bad') sentry.send_response_to_sentry(error_response, response.errors['code']) Args: response (Response): Response object to send to Sentry. sentry_message (str): Message to be displayed in Sentry. Best practice for errors is for the sentry_message to be the error code so that all errors of the same type are grouped together. """ with sentry_sdk.new_scope() as scope: scope.set_extra('message', response.message) scope.set_extra('errors', response.errors) scope.set_extra('status', response.status) sentry_sdk.capture_message(sentry_message) init_sentry()