Skip to content

Commit a31c54f

Browse files
authored
fix: Open relevant error when SpotlightMiddleware is on (#3614)
This fixes an issue with the recent SpotlightMiddleware patch where the error triggered the page was not highlighted/opened automatically. It changes the semapics of `capture_event` and methods depending on this a bit: we now return the event_id if the error is sent to spotlight even if it was not sent upstream to Sentry servers.
1 parent be64348 commit a31c54f

File tree

2 files changed

+22
-15
lines changed

2 files changed

+22
-15
lines changed

Diff for: sentry_sdk/client.py

+9-10
Original file line numberDiff line numberDiff line change
@@ -753,18 +753,16 @@ def capture_event(
753753
754754
:returns: An event ID. May be `None` if there is no DSN set or of if the SDK decided to discard the event for other reasons. In such situations setting `debug=True` on `init()` may help.
755755
"""
756-
if hint is None:
757-
hint = {}
758-
event_id = event.get("event_id")
759756
hint = dict(hint or ()) # type: Hint
760757

761-
if event_id is None:
762-
event["event_id"] = event_id = uuid.uuid4().hex
763758
if not self._should_capture(event, hint, scope):
764759
return None
765760

766761
profile = event.pop("profile", None)
767762

763+
event_id = event.get("event_id")
764+
if event_id is None:
765+
event["event_id"] = event_id = uuid.uuid4().hex
768766
event_opt = self._prepare_event(event, hint, scope)
769767
if event_opt is None:
770768
return None
@@ -812,15 +810,16 @@ def capture_event(
812810
for attachment in attachments or ():
813811
envelope.add_item(attachment.to_envelope_item())
814812

813+
return_value = None
815814
if self.spotlight:
816815
self.spotlight.capture_envelope(envelope)
816+
return_value = event_id
817817

818-
if self.transport is None:
819-
return None
820-
821-
self.transport.capture_envelope(envelope)
818+
if self.transport is not None:
819+
self.transport.capture_envelope(envelope)
820+
return_value = event_id
822821

823-
return event_id
822+
return return_value
824823

825824
def capture_session(
826825
self, session # type: Session

Diff for: sentry_sdk/spotlight.py

+13-5
Original file line numberDiff line numberDiff line change
@@ -79,14 +79,22 @@ def process_exception(self, _request, exception):
7979
spotlight_url = spotlight_client.url.rsplit("/", 1)[0]
8080

8181
try:
82-
spotlight = (
83-
urllib.request.urlopen(spotlight_url).read().decode("utf-8")
84-
).replace("<html>", f'<html><base href="{spotlight_url}">')
82+
spotlight = urllib.request.urlopen(spotlight_url).read().decode("utf-8")
8583
except urllib.error.URLError:
8684
return None
8785
else:
88-
sentry_sdk.api.capture_exception(exception)
89-
return HttpResponseServerError(spotlight)
86+
event_id = sentry_sdk.api.capture_exception(exception)
87+
return HttpResponseServerError(
88+
spotlight.replace(
89+
"<html>",
90+
(
91+
f'<html><base href="{spotlight_url}">'
92+
'<script>window.__spotlight = {{ initOptions: {{ startFrom: "/errors/{event_id}" }}}};</script>'.format(
93+
event_id=event_id
94+
)
95+
),
96+
)
97+
)
9098

9199
except ImportError:
92100
settings = None

0 commit comments

Comments
 (0)