Skip to content

Commit 6b4d3ec

Browse files
authored
Drop events, breadcrumbs after faulty callbacks (#7707)
If `before_breadcrumb`, an event processor, or an error processor raises an exception, the associated breadcrumb or event should be discarded as per spec. Change goes into new major since it's breaking.
1 parent 58ea973 commit 6b4d3ec

2 files changed

Lines changed: 48 additions & 7 deletions

File tree

‎sentry_sdk/scope.py‎

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -919,6 +919,7 @@ def add_breadcrumb(
919919

920920
new_crumb = crumb
921921
if before_breadcrumb is not None:
922+
new_crumb = None
922923
with capture_internal_exceptions():
923924
new_crumb = before_breadcrumb(crumb, hint)
924925

@@ -1480,7 +1481,7 @@ def run_error_processors(self, event: "Event", hint: "Hint") -> "Optional[Event]
14801481
)
14811482

14821483
for error_processor in error_processors:
1483-
new_event = event
1484+
new_event = None
14841485
with capture_internal_exceptions():
14851486
new_event = error_processor(event, exc_info)
14861487
if new_event is None:
@@ -1510,7 +1511,7 @@ def run_event_processors(self, event: "Event", hint: "Hint") -> "Optional[Event]
15101511
)
15111512

15121513
for event_processor in event_processors:
1513-
new_event = event
1514+
new_event = None
15141515
with capture_internal_exceptions():
15151516
new_event = event_processor(event, hint)
15161517
if new_event is None:

‎tests/test_basics.py‎

Lines changed: 45 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -214,7 +214,7 @@ def do_this():
214214

215215
@pytest.mark.tests_internal_exceptions
216216
def test_option_before_breadcrumb_exception(sentry_init, capture_events):
217-
"""Exceptions in before_breadcrumb are contained."""
217+
"""Breadcrumb is dropped if before_breadcrumb raises an exception."""
218218

219219
def before_breadcrumb(crumb, hint):
220220
1 / 0
@@ -234,10 +234,7 @@ def do_this():
234234
(event,) = events
235235

236236
assert event["exception"]["values"][0]["type"] == "ValueError"
237-
(crumb,) = event["breadcrumbs"]["values"]
238-
assert "timestamp" in crumb
239-
assert crumb["message"] == "Hello"
240-
assert crumb["type"] == "default"
237+
assert event["breadcrumbs"]["values"] == []
241238

242239

243240
def test_breadcrumb_arguments(sentry_init, capture_events):
@@ -661,6 +658,49 @@ def foo(event, hint):
661658
sentry_sdk.scope.global_event_processors = old_processors
662659

663660

661+
@pytest.mark.tests_internal_exceptions
662+
def test_event_processor_exception_drops_event_and_records_client_report(
663+
sentry_init, capture_events, capture_record_lost_event_calls
664+
):
665+
sentry_init(default_integrations=False)
666+
events = capture_events()
667+
record_lost_event_calls = capture_record_lost_event_calls()
668+
669+
scope = sentry_sdk.get_isolation_scope()
670+
671+
@scope.add_event_processor
672+
def bad_processor(event, hint):
673+
raise ValueError("processor error")
674+
675+
capture_message("should be dropped")
676+
677+
assert len(events) == 0
678+
assert ("event_processor", "error", None, 1) in record_lost_event_calls
679+
680+
681+
@pytest.mark.tests_internal_exceptions
682+
def test_error_processor_exception_drops_event(
683+
sentry_init, capture_events, capture_record_lost_event_calls
684+
):
685+
sentry_init(default_integrations=False)
686+
events = capture_events()
687+
record_lost_event_calls = capture_record_lost_event_calls()
688+
689+
scope = sentry_sdk.get_isolation_scope()
690+
691+
@scope.add_error_processor
692+
def bad_error_processor(event, exc_info):
693+
raise ValueError("error processor error")
694+
695+
try:
696+
raise ValueError("original error")
697+
except Exception:
698+
capture_exception()
699+
700+
assert len(events) == 0
701+
assert ("event_processor", "error", None, 1) in record_lost_event_calls
702+
703+
664704
@pytest.mark.tests_internal_exceptions
665705
def test_before_send_exception_records_callback_error(
666706
sentry_init, capture_events, capture_record_lost_event_calls

0 commit comments

Comments
 (0)