Skip to content

Commit 44d1619

Browse files
feat(asgi): Add option to disable suppressing chained exceptions
1 parent f963475 commit 44d1619

File tree

1 file changed

+27
-4
lines changed

1 file changed

+27
-4
lines changed

sentry_sdk/integrations/asgi.py

Lines changed: 27 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
Based on Tom Christie's `sentry-asgi <https://github.com/encode/sentry-asgi>`.
55
"""
66

7+
import sys
78
import asyncio
89
import inspect
910
from copy import deepcopy
@@ -37,6 +38,8 @@
3738
logger,
3839
transaction_from_function,
3940
_get_installed_modules,
41+
reraise,
42+
capture_internal_exceptions,
4043
)
4144

4245
from typing import TYPE_CHECKING
@@ -103,6 +106,7 @@ def __init__(
103106
span_origin: str = "manual",
104107
http_methods_to_capture: "Tuple[str, ...]" = DEFAULT_HTTP_METHODS_TO_CAPTURE,
105108
asgi_version: "Optional[int]" = None,
109+
suppress_chained_exceptions: "Optional[bool]" = None,
106110
) -> None:
107111
"""
108112
Instrument an ASGI application with Sentry. Provides HTTP/websocket
@@ -139,6 +143,7 @@ def __init__(
139143
self.span_origin = span_origin
140144
self.app = app
141145
self.http_methods_to_capture = http_methods_to_capture
146+
self.suppress_chained_exceptions = suppress_chained_exceptions
142147

143148
if asgi_version is None:
144149
if _looks_like_asgi3(app):
@@ -187,8 +192,17 @@ async def _run_app(
187192
return await self.app(scope, receive, send)
188193

189194
except Exception as exc:
190-
self._capture_lifespan_exception(exc)
191-
raise exc from None
195+
if (
196+
self.suppress_chained_exceptions is None
197+
or self.suppress_chained_exceptions
198+
):
199+
self._capture_lifespan_exception(exc)
200+
raise exc from None
201+
202+
exc_info = sys.exc_info()
203+
with capture_internal_exceptions():
204+
self._capture_request_exception(exc)
205+
reraise(*exc_info)
192206

193207
client = sentry_sdk.get_client()
194208
span_streaming = has_span_streaming_enabled(client.options)
@@ -323,8 +337,17 @@ async def _sentry_wrapped_send(
323337
scope, receive, _sentry_wrapped_send
324338
)
325339
except Exception as exc:
326-
self._capture_request_exception(exc)
327-
raise exc from None
340+
if (
341+
self.suppress_chained_exceptions is None
342+
or self.suppress_chained_exceptions
343+
):
344+
self._capture_lifespan_exception(exc)
345+
raise exc from None
346+
347+
exc_info = sys.exc_info()
348+
with capture_internal_exceptions():
349+
self._capture_request_exception(exc)
350+
reraise(*exc_info)
328351
finally:
329352
_asgi_middleware_applied.set(False)
330353

0 commit comments

Comments
 (0)