|
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 |
|
38 | 39 | logger, |
39 | 40 | transaction_from_function, |
40 | 41 | _get_installed_modules, |
| 42 | + reraise, |
| 43 | + capture_internal_exceptions, |
41 | 44 | ) |
42 | 45 |
|
43 | 46 | from typing import TYPE_CHECKING |
@@ -188,8 +191,19 @@ async def _run_app( |
188 | 191 | return await self.app(scope, receive, send) |
189 | 192 |
|
190 | 193 | except Exception as exc: |
191 | | - self._capture_lifespan_exception(exc) |
192 | | - raise exc from None |
| 194 | + suppress_chained_exceptions = ( |
| 195 | + sentry_sdk.get_client() |
| 196 | + .options.get("_experiments", {}) |
| 197 | + .get("suppress_asgi_chained_exceptions", True) |
| 198 | + ) |
| 199 | + if suppress_chained_exceptions: |
| 200 | + self._capture_lifespan_exception(exc) |
| 201 | + raise exc from None |
| 202 | + |
| 203 | + exc_info = sys.exc_info() |
| 204 | + with capture_internal_exceptions(): |
| 205 | + self._capture_lifespan_exception(exc) |
| 206 | + reraise(*exc_info) |
193 | 207 |
|
194 | 208 | client = sentry_sdk.get_client() |
195 | 209 | span_streaming = has_span_streaming_enabled(client.options) |
@@ -326,8 +340,19 @@ async def _sentry_wrapped_send( |
326 | 340 | scope, receive, _sentry_wrapped_send |
327 | 341 | ) |
328 | 342 | except Exception as exc: |
329 | | - self._capture_request_exception(exc) |
330 | | - raise exc from None |
| 343 | + suppress_chained_exceptions = ( |
| 344 | + sentry_sdk.get_client() |
| 345 | + .options.get("_experiments", {}) |
| 346 | + .get("suppress_asgi_chained_exceptions", True) |
| 347 | + ) |
| 348 | + if suppress_chained_exceptions: |
| 349 | + self._capture_request_exception(exc) |
| 350 | + raise exc from None |
| 351 | + |
| 352 | + exc_info = sys.exc_info() |
| 353 | + with capture_internal_exceptions(): |
| 354 | + self._capture_request_exception(exc) |
| 355 | + reraise(*exc_info) |
331 | 356 | finally: |
332 | 357 | _asgi_middleware_applied.set(False) |
333 | 358 |
|
|
0 commit comments