|
4 | 4 | Based on Tom Christie's `sentry-asgi <https://github.com/encode/sentry-asgi>`. |
5 | 5 | """ |
6 | 6 |
|
| 7 | +import sys |
7 | 8 | import asyncio |
8 | 9 | import inspect |
9 | 10 | from copy import deepcopy |
|
37 | 38 | logger, |
38 | 39 | transaction_from_function, |
39 | 40 | _get_installed_modules, |
| 41 | + reraise, |
| 42 | + capture_internal_exceptions, |
40 | 43 | ) |
41 | 44 |
|
42 | 45 | from typing import TYPE_CHECKING |
@@ -103,6 +106,7 @@ def __init__( |
103 | 106 | span_origin: str = "manual", |
104 | 107 | http_methods_to_capture: "Tuple[str, ...]" = DEFAULT_HTTP_METHODS_TO_CAPTURE, |
105 | 108 | asgi_version: "Optional[int]" = None, |
| 109 | + suppress_chained_exceptions: "Optional[bool]" = None, |
106 | 110 | ) -> None: |
107 | 111 | """ |
108 | 112 | Instrument an ASGI application with Sentry. Provides HTTP/websocket |
@@ -139,6 +143,7 @@ def __init__( |
139 | 143 | self.span_origin = span_origin |
140 | 144 | self.app = app |
141 | 145 | self.http_methods_to_capture = http_methods_to_capture |
| 146 | + self.suppress_chained_exceptions = suppress_chained_exceptions |
142 | 147 |
|
143 | 148 | if asgi_version is None: |
144 | 149 | if _looks_like_asgi3(app): |
@@ -187,8 +192,17 @@ async def _run_app( |
187 | 192 | return await self.app(scope, receive, send) |
188 | 193 |
|
189 | 194 | 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) |
192 | 206 |
|
193 | 207 | client = sentry_sdk.get_client() |
194 | 208 | span_streaming = has_span_streaming_enabled(client.options) |
@@ -323,8 +337,17 @@ async def _sentry_wrapped_send( |
323 | 337 | scope, receive, _sentry_wrapped_send |
324 | 338 | ) |
325 | 339 | 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) |
328 | 351 | finally: |
329 | 352 | _asgi_middleware_applied.set(False) |
330 | 353 |
|
|
0 commit comments