Skip to content

Commit 3140d17

Browse files
authored
feat: Pass hints to before send and event processors (#60)
1 parent 4dd2327 commit 3140d17

File tree

7 files changed

+19
-8
lines changed

7 files changed

+19
-8
lines changed

sentry_sdk/client.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
convert_types,
1111
handle_in_app,
1212
get_type_name,
13+
capture_internal_exceptions,
1314
logger,
1415
)
1516
from sentry_sdk.transport import make_transport
@@ -87,7 +88,8 @@ def _prepare_event(self, event, hint, scope):
8788

8889
before_send = self.options["before_send"]
8990
if before_send is not None:
90-
new_event = before_send(event)
91+
with capture_internal_exceptions():
92+
new_event = before_send(event, hint)
9193
if new_event is None:
9294
logger.info("before send dropped event (%s)", event)
9395
event = new_event

sentry_sdk/integrations/_wsgi.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -202,7 +202,7 @@ def close(self):
202202

203203

204204
def _make_wsgi_event_processor(environ):
205-
def event_processor(event):
205+
def event_processor(event, hint):
206206
with capture_internal_exceptions():
207207
# if the code below fails halfway through we at least have some data
208208
request_info = event.setdefault("request", {})

sentry_sdk/integrations/django.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ def sentry_patched_get_response(self, request):
6363

6464

6565
def _make_event_processor(weak_request):
66-
def event_processor(event):
66+
def event_processor(event, hint):
6767
# if the request is gone we are fine not logging the data from
6868
# it. This might happen if the processor is pushed away to
6969
# another thread.

sentry_sdk/integrations/flask.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ def _request_started(sender, **kwargs):
6161
scope.add_event_processor(_make_request_event_processor(app, weak_request))
6262

6363

64-
def event_processor(event):
64+
def event_processor(event, hint):
6565
request = getattr(_request_ctx_stack.top, "request", None)
6666

6767
if request:
@@ -109,7 +109,7 @@ def _capture_exception(sender, exception, **kwargs):
109109

110110

111111
def _make_request_event_processor(app, weak_request):
112-
def inner(event):
112+
def inner(event, hint):
113113
request = weak_request()
114114

115115
# if the request is gone we are fine not logging the data from

sentry_sdk/scope.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
from sentry_sdk.utils import logger
1+
from sentry_sdk.utils import logger, capture_internal_exceptions
22

33

44
def _attr_setter(fn):
@@ -140,7 +140,8 @@ def _drop(event, cause, ty):
140140
event = new_event
141141

142142
for processor in self._event_processors:
143-
new_event = processor(event)
143+
with capture_internal_exceptions():
144+
new_event = processor(event, hint)
144145
if new_event is None:
145146
return _drop(event, processor, "event processor")
146147
event = new_event

sentry_sdk/utils.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,12 @@ class EventHint(object):
4242
def __init__(self, exc_info=None):
4343
self.exc_info = exc_info
4444

45+
@property
46+
def exception(self):
47+
"""Returns the exception value on the hint if there is one."""
48+
if self.exc_info is not None:
49+
return self.exc_info[1]
50+
4551
@classmethod
4652
def with_exc_info(cls, exc_info=None):
4753
"""Creates a hint with the exc info filled in."""

tests/test_basics.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,9 @@ def test_option_callback(sentry_init, capture_events):
5050
drop_events = False
5151
drop_breadcrumbs = False
5252

53-
def before_send(event):
53+
def before_send(event, hint):
54+
assert hint is not None
55+
assert isinstance(hint.exception, ValueError)
5456
if not drop_events:
5557
event["extra"] = {"foo": "bar"}
5658
return event

0 commit comments

Comments
 (0)