diff --git a/ingest/views.py b/ingest/views.py index 2bce194..680c268 100644 --- a/ingest/views.py +++ b/ingest/views.py @@ -179,11 +179,11 @@ class IngestEnvelopeAPIView(BaseIngestAPIView): if len(request.data) != 3: # multi-part envelopes trigger an error too - sentry_sdk_extensions.capture_stacktrace("Invalid envelope (not 3 parts)") + sentry_sdk_extensions.capture_stacktrace_2("Invalid envelope (not 3 parts)") return Response({"message": "Missing headers / unsupported type"}, status=status.HTTP_501_NOT_IMPLEMENTED) if request.data[1].get("type") != "event": - sentry_sdk_extensions.capture_stacktrace("Invalid envelope (not an event)") + sentry_sdk_extensions.capture_stacktrace_2("Invalid envelope (not an event)") return Response({"message": "Only events are supported"}, status=status.HTTP_501_NOT_IMPLEMENTED) # TODO think about a good order to handle this in. Namely: if no project Header is provided, you are basically diff --git a/sentry_sdk_extensions/__init__.py b/sentry_sdk_extensions/__init__.py index f28f143..35f7ee5 100644 --- a/sentry_sdk_extensions/__init__.py +++ b/sentry_sdk_extensions/__init__.py @@ -2,6 +2,10 @@ from sentry_sdk.utils import capture_internal_exceptions, current_stacktrace import sentry_sdk +class AdHocException(Exception): + pass + + def capture_stacktrace(message): """ Capture the current stacktrace and send it to Sentry; the standard sentry_sdk does not provide this; it either @@ -26,3 +30,17 @@ def capture_stacktrace(message): event["level"] = "error" event["logentry"] = {"message": message} sentry_sdk.capture_event(event) + + +def capture_stacktrace_2(message): + """ + Capture the current stacktrace and send it to Sentry. Like capture_stacktrace, but using an "Ad Hoc" Exception. + This allows us to use the standard sentry_sdk.capture_exception function, and our stacktrace to show up as an + exception on the server-side. + + In good old Python tradition, we've just tacked a "_2" onto the end of the function name. + """ + try: + raise AdHocException(message) + except AdHocException as e: + sentry_sdk.capture_exception(e)