diff --git a/scripts/find_raise_from_none.py b/scripts/find_raise_from_none.py index 63b2b84333..089a14a1df 100644 --- a/scripts/find_raise_from_none.py +++ b/scripts/find_raise_from_none.py @@ -42,17 +42,11 @@ def main(): for module_path in walk_package_modules(): scan_file(module_path) - # TODO: Investigate why we suppress exception chains here. - ignored_raises = { - pathlib.Path("sentry_sdk/integrations/asgi.py"): 2, - pathlib.Path("sentry_sdk/integrations/asyncio.py"): 1, - } - raise_from_none_count = { file: len(occurences) for file, occurences in RaiseFromNoneVisitor.line_numbers.items() } - if raise_from_none_count != ignored_raises: + if raise_from_none_count: exc = Exception("Detected unexpected raise ... from None.") exc.add_note( "Raise ... from None suppresses chained exceptions, removing valuable context." diff --git a/sentry_sdk/integrations/asgi.py b/sentry_sdk/integrations/asgi.py index da87f8b2fb..655b7e69fc 100644 --- a/sentry_sdk/integrations/asgi.py +++ b/sentry_sdk/integrations/asgi.py @@ -4,6 +4,7 @@ Based on Tom Christie's `sentry-asgi `. """ +import sys import asyncio import inspect from copy import deepcopy @@ -37,6 +38,8 @@ logger, transaction_from_function, _get_installed_modules, + capture_internal_exceptions, + reraise, ) from typing import TYPE_CHECKING @@ -187,8 +190,10 @@ async def _run_app( return await self.app(scope, receive, send) except Exception as exc: - self._capture_lifespan_exception(exc) - raise exc from None + exc_info = sys.exc_info() + with capture_internal_exceptions(): + self._capture_lifespan_exception(exc) + reraise(*exc_info) client = sentry_sdk.get_client() span_streaming = has_span_streaming_enabled(client.options) @@ -323,8 +328,10 @@ async def _sentry_wrapped_send( scope, receive, _sentry_wrapped_send ) except Exception as exc: - self._capture_request_exception(exc) - raise exc from None + exc_info = sys.exc_info() + with capture_internal_exceptions(): + self._capture_request_exception(exc) + reraise(*exc_info) finally: _asgi_middleware_applied.set(False) diff --git a/sentry_sdk/integrations/asyncio.py b/sentry_sdk/integrations/asyncio.py index b7aa0a7202..f01b3d8732 100644 --- a/sentry_sdk/integrations/asyncio.py +++ b/sentry_sdk/integrations/asyncio.py @@ -78,8 +78,8 @@ async def _task_with_sentry_span_creation() -> "Any": ): try: result = await coro - except StopAsyncIteration as e: - raise e from None + except StopAsyncIteration: + raise except Exception: reraise(*_capture_exception())