def exception_handler(type, value, traceback): settings_diff = diff_config( load_config(DEFAULT_CONFIG, defaults=False), config, exclude=set([("lancet", "sentry_dsn")]), ) sys.__excepthook__(type, value, traceback) if type in IGNORED_EXCEPTIONS: return click.echo() hr(fg="yellow") click.secho( "\nAs requested, I am sending details about this " "error to Sentry, please report the following ID " "when seeking support:" ) exc_info = (type, value, traceback) event, hint = event_from_exception( exc_info, client_options=sentry_client.options ) event.setdefault("extra", {}).update( { "settings": as_dict(settings_diff), "working_dir": os.getcwd(), } ) error_id = sentry_client.capture_event(event, hint=hint) click.secho("\n {}\n".format(error_id), fg="yellow")
def sentry_handler(event, context, *args, **kwargs): # type: (Any, Any, *Any, **Any) -> Any hub = Hub.current integration = hub.get_integration(AwsLambdaIntegration) if integration is None: return handler(event, context, *args, **kwargs) # If an integration is there, a client has to be there. client = hub.client # type: Any configured_time = context.get_remaining_time_in_millis() with hub.push_scope() as scope: with capture_internal_exceptions(): scope.clear_breadcrumbs() scope.add_event_processor( _make_request_event_processor(event, context, configured_time)) scope.set_tag("aws_region", context.invoked_function_arn.split(":")[3]) # Starting the Timeout thread only if the configured time is greater than Timeout warning # buffer and timeout_warning parameter is set True. if (integration.timeout_warning and configured_time > TIMEOUT_WARNING_BUFFER): waiting_time = (configured_time - TIMEOUT_WARNING_BUFFER) / MILLIS_TO_SECONDS timeout_thread = TimeoutThread( waiting_time, configured_time / MILLIS_TO_SECONDS) # Starting the thread to raise timeout warning exception timeout_thread.start() headers = event.get("headers", {}) transaction = Transaction.continue_from_headers( headers, op="serverless.function", name=context.function_name) with hub.start_transaction(transaction): try: return handler(event, context, *args, **kwargs) except Exception: exc_info = sys.exc_info() event, hint = event_from_exception( exc_info, client_options=client.options, mechanism={ "type": "aws_lambda", "handled": False }, ) hub.capture_event(event, hint=hint) reraise(*exc_info)
def _emit(self, record): # type: (LogRecord) -> None if not _can_record(record): return hub = Hub.current if hub.client is None: return client_options = hub.client.options # exc_info might be None or (None, None, None) if record.exc_info is not None and record.exc_info[0] is not None: event, hint = event_from_exception( record.exc_info, client_options=client_options, mechanism={ "type": "logging", "handled": True }, ) elif record.exc_info and record.exc_info[0] is None: event = {} hint = {} with capture_internal_exceptions(): event["threads"] = { "values": [{ "stacktrace": current_stacktrace(client_options["with_locals"]), "crashed": False, "current": True, }] } else: event = {} hint = {} hint["log_record"] = record event["level"] = _logging_to_event_level(record.levelname) event["logger"] = record.name event["logentry"] = { "message": to_string(record.msg), "params": record.args } event["extra"] = _extra_from_record(record) hub.capture_event(event, hint=hint)
def _got_request_exception(request=None, **kwargs): # type: (WSGIRequest, **Any) -> None hub = Hub.current integration = hub.get_integration(DjangoIntegration) if integration is not None: event, hint = event_from_exception( sys.exc_info(), client_options=hub.client.options, mechanism={ "type": "django", "handled": False }, ) hub.capture_event(event, hint=hint)
def _capture_exception(ty, value, tb): hub = Hub.current if hub.get_integration(TornadoIntegration) is None: return if isinstance(value, HTTPError): return event, hint = event_from_exception( (ty, value, tb), client_options=hub.client.options, mechanism={"type": "tornado", "handled": False}, ) hub.capture_event(event, hint=hint)
def _capture_exception(exc_info, **kwargs): hub = Hub.current if hub.get_integration(RqIntegration) is None: return event, hint = event_from_exception( exc_info, client_options=hub.client.options, mechanism={ "type": "rq", "handled": False }, ) hub.capture_event(event, hint=hint)
def _capture_exception(hub, exc): # type: (Hub, Any) -> None # Check client here as it might have been unset while streaming response if hub.client is not None: event, hint = event_from_exception( exc, client_options=hub.client.options, mechanism={ "type": "asgi", "handled": False }, ) hub.capture_event(event, hint=hint)
def _capture_exception(exc_info, **kwargs): # type: (ExcInfo, **Any) -> None if exc_info[0] is None or issubclass(exc_info[0], HTTPException): return hub = Hub.current if hub.get_integration(PyramidIntegration) is None: return event, hint = event_from_exception( exc_info, client_options=hub.client.options, mechanism={"type": "pyramid", "handled": False}, ) hub.capture_event(event, hint=hint)
def sentry_sdk_excepthook(exctype, value, traceback): with capture_internal_exceptions(): hub = Hub.current event, hint = event_from_exception( (exctype, value, traceback), with_locals=hub.client.options["with_locals"], mechanism={ "type": "excepthook", "handled": False }, ) hub.capture_event(event, hint=hint) return old_excepthook(exctype, value, traceback)
def _capture_and_reraise(): exc_info = sys.exc_info() hub = Hub.current if hub is not None and hub.client is not None: event, hint = event_from_exception( exc_info, client_options=hub.client.options, mechanism={ "type": "serverless", "handled": False }, ) hub.capture_event(event, hint=hint) reraise(*exc_info)
def _capture_exception(hub): # type: (Hub) -> ExcInfo # Check client here as it might have been unset while streaming response if hub.client is not None: exc_info = sys.exc_info() event, hint = event_from_exception( exc_info, client_options=hub.client.options, mechanism={ "type": "wsgi", "handled": False }, ) hub.capture_event(event, hint=hint) return exc_info
def _capture_exception(sender, exception, **kwargs): # type: (Flask, Union[ValueError, BaseException], **Any) -> None hub = Hub.current if hub.get_integration(FlaskIntegration) is None: return event, hint = event_from_exception( exception, client_options=hub.client.options, mechanism={ "type": "flask", "handled": False }, ) hub.capture_event(event, hint=hint)
def _capture_exception(exc_info, **kwargs): # type: (ExcInfo, **Any) -> None hub = Hub.current if hub.get_integration(RqIntegration) is None: return # If an integration is there, a client has to be there. client = hub.client # type: Any event, hint = event_from_exception( exc_info, client_options=client.options, mechanism={"type": "rq", "handled": False}, ) hub.capture_event(event, hint=hint)
def _capture_exception(exception): if isinstance(exception, SanicException): return hub = Hub.current integration = hub.get_integration(SanicIntegration) if integration is None: return with capture_internal_exceptions(): event, hint = event_from_exception( exception, client_options=hub.client.options, mechanism={"type": "sanic", "handled": False}, ) hub.capture_event(event, hint=hint)
def _capture_exception(exc_info, hub): """ Send Beam exception to Sentry. """ integration = hub.get_integration(BeamIntegration) if integration: client = hub.client event, hint = event_from_exception( exc_info, client_options=client.options, mechanism={ "type": "beam", "handled": False }, ) hub.capture_event(event, hint=hint)
def _capture_exception(): hub = Hub.current exc_info = sys.exc_info() if hub.get_integration(CeleryIntegration) is not None: event, hint = event_from_exception( exc_info, client_options=hub.client.options, mechanism={ "type": "celery", "handled": False }, ) hub.capture_event(event, hint=hint) return exc_info
def sentry_sdk_excepthook(exctype, value, traceback): hub = Hub.current integration = hub.get_integration(ExcepthookIntegration) if integration is not None and _should_send(): with capture_internal_exceptions(): event, hint = event_from_exception( (exctype, value, traceback), client_options=hub.client.options, mechanism={ "type": "excepthook", "handled": False }, ) hub.capture_event(event, hint=hint) return old_excepthook(exctype, value, traceback)
def wrapped_callback(*args, **kwargs): # type: (*object, **object) -> Any try: res = prepared_callback(*args, **kwargs) except HTTPResponse: raise except Exception as exception: event, hint = event_from_exception( exception, client_options=client.options, mechanism={"type": "bottle", "handled": False}, ) hub.capture_event(event, hint=hint) raise exception return res
def handle_error(self, context, handler, exception): req = handler.request exc_info = exc_info_from_error(exception) with sentry_sdk.push_scope() as scope: event, hint = event_from_exception(exc_info) event["request"] = { "url": req.full_url(), "method": req.method, "data": req.arguments, "query_string": req.query, "headers": req.headers, "body": req.body, } scope.set_extra('thumbor-version', __version__) sentry_sdk.capture_event(event, hint=hint)
def _capture_exception(exception): # type: (Union[Tuple[Optional[type], Optional[BaseException], Any], BaseException]) -> None hub = Hub.current integration = hub.get_integration(SanicIntegration) if integration is None: return # If an integration is there, a client has to be there. client = hub.client # type: Any with capture_internal_exceptions(): event, hint = event_from_exception( exception, client_options=client.options, mechanism={"type": "sanic", "handled": False}, ) hub.capture_event(event, hint=hint)
def _capture_exception(exc_info): # type: (ExcInfo) -> None if exc_info[0] is None or issubclass(exc_info[0], HTTPException): return hub = Hub.current if hub.get_integration(PyramidIntegration) is None: return # If an integration is there, a client has to be there. client = hub.client # type: Any event, hint = event_from_exception( exc_info, client_options=client.options, mechanism={"type": "pyramid", "handled": False}, ) hub.capture_event(event, hint=hint)
def _capture_exception(ty, value, tb): # type: (type, BaseException, Any) -> None hub = Hub.current if hub.get_integration(TornadoIntegration) is None: return if isinstance(value, HTTPError): return # If an integration is there, a client has to be there. client = hub.client # type: Any event, hint = event_from_exception( (ty, value, tb), client_options=client.options, mechanism={"type": "tornado", "handled": False}, ) hub.capture_event(event, hint=hint)
def _capture_exception(sender, exception, **kwargs): # type: (Quart, Union[ValueError, BaseException], **Any) -> None hub = Hub.current if hub.get_integration(QuartIntegration) is None: return # If an integration is there, a client has to be there. client = hub.client # type: Any event, hint = event_from_exception( exception, client_options=client.options, mechanism={ "type": "quart", "handled": False }, ) hub.capture_event(event, hint=hint)
def handle_error(self, context, handler, exception): # pylint: disable=unused-argument req = handler.request exc_info = exc_info_from_error(exception) with sentry_sdk.push_scope() as scope: event, hint = event_from_exception(exc_info) event["request"] = { "url": req.full_url(), "method": req.method, "data": req.arguments, "query_string": req.query, "headers": req.headers, "body": req.body, } scope.set_extra("thumbor-version", __version__) scope.set_extra("thumbor-loader", self.config.LOADER) scope.set_extra("thumbor-storage", self.config.STORAGE) sentry_sdk.capture_event(event, hint=hint)
def _capture_exception(): hub = Hub.current exc_info = sys.exc_info() if hub.get_integration(ThreadingIntegration) is not None: # If an integration is there, a client has to be there. client = hub.client # type: Any event, hint = event_from_exception( exc_info, client_options=client.options, mechanism={ "type": "threading", "handled": False }, ) hub.capture_event(event, hint=hint) return exc_info
def _capture_exception(exc_info, hub): # type: (ExcInfo, Hub) -> None """ Send Beam exception to Sentry. """ integration = hub.get_integration(BeamIntegration) if integration is None: return client = hub.client if client is None: return event, hint = event_from_exception( exc_info, client_options=client.options, mechanism={"type": "beam", "handled": False}, ) hub.capture_event(event, hint=hint)
def _capture_exception(hub): # type: (Hub) -> ExcInfo exc_info = sys.exc_info() # Check client here as it might have been unset while streaming response if hub.client is not None: e = exc_info[1] # SystemExit(0) is the only uncaught exception that is expected behavior should_skip_capture = isinstance(e, SystemExit) and e.code in (0, None) if not should_skip_capture: event, hint = event_from_exception( exc_info, client_options=hub.client.options, mechanism={"type": "wsgi", "handled": False}, ) hub.capture_event(event, hint=hint) return exc_info
def _sendCrashReport(self): # Before sending data, the user comments are stored self.data["user_info"] = self.user_description_text_area.toPlainText() try: hub = Hub.current event, hint = event_from_exception( (self.exception_type, self.value, self.traceback)) hub.capture_event(event, hint=hint) hub.flush() except Exception as e: # We don't want any exception to cause problems Logger.logException( "e", "An exception occurred while trying to send crash report") if not self.has_started: print( "An exception occurred while trying to send crash report: %s" % e) os._exit(1)
def _capture_exception(task, exc_info): hub = Hub.current if hub.get_integration(CeleryIntegration) is None: return if isinstance(exc_info[1], CELERY_CONTROL_FLOW_EXCEPTIONS): return if hasattr(task, "throws") and isinstance(exc_info[1], task.throws): return # If an integration is there, a client has to be there. client = hub.client # type: Any event, hint = event_from_exception( exc_info, client_options=client.options, mechanism={"type": "celery", "handled": False}, ) hub.capture_event(event, hint=hint)
def capture_exception(self, error=None): """Captures an exception. The argument passed can be `None` in which case the last exception will be reported, otherwise an exception object or an `exc_info` tuple. """ client = self.client if client is None: return if error is None: exc_info = sys.exc_info() else: exc_info = exc_info_from_error(error) event, hint = event_from_exception(exc_info, client_options=client.options) try: return self.capture_event(event, hint=hint) except Exception: self._capture_internal_exception(sys.exc_info())
def sentry_sdk_excepthook(type_, value, traceback): # type: (Type[BaseException], BaseException, TracebackType) -> None hub = Hub.current integration = hub.get_integration(ExcepthookIntegration) if integration is not None and _should_send(integration.always_run): # If an integration is there, a client has to be there. client = hub.client # type: Any with capture_internal_exceptions(): event, hint = event_from_exception( (type_, value, traceback), client_options=client.options, mechanism={ "type": "excepthook", "handled": False }, ) hub.capture_event(event, hint=hint) return old_excepthook(type_, value, traceback)