Skip to content

Commit 7d8d3d1

Browse files
fix: Do not raise from None
1 parent 5896507 commit 7d8d3d1

File tree

3 files changed

+13
-12
lines changed

3 files changed

+13
-12
lines changed

scripts/find_raise_from_none.py

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -42,17 +42,11 @@ def main():
4242
for module_path in walk_package_modules():
4343
scan_file(module_path)
4444

45-
# TODO: Investigate why we suppress exception chains here.
46-
ignored_raises = {
47-
pathlib.Path("sentry_sdk/integrations/asgi.py"): 2,
48-
pathlib.Path("sentry_sdk/integrations/asyncio.py"): 1,
49-
}
50-
5145
raise_from_none_count = {
5246
file: len(occurences)
5347
for file, occurences in RaiseFromNoneVisitor.line_numbers.items()
5448
}
55-
if raise_from_none_count != ignored_raises:
49+
if raise_from_none_count:
5650
exc = Exception("Detected unexpected raise ... from None.")
5751
exc.add_note(
5852
"Raise ... from None suppresses chained exceptions, removing valuable context."

sentry_sdk/integrations/asgi.py

Lines changed: 11 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+
capture_internal_exceptions,
42+
reraise,
4043
)
4144

4245
from typing import TYPE_CHECKING
@@ -187,8 +190,10 @@ async def _run_app(
187190
return await self.app(scope, receive, send)
188191

189192
except Exception as exc:
190-
self._capture_lifespan_exception(exc)
191-
raise exc from None
193+
exc_info = sys.exc_info()
194+
with capture_internal_exceptions():
195+
self._capture_lifespan_exception(exc)
196+
reraise(*exc_info)
192197

193198
client = sentry_sdk.get_client()
194199
span_streaming = has_span_streaming_enabled(client.options)
@@ -323,8 +328,10 @@ async def _sentry_wrapped_send(
323328
scope, receive, _sentry_wrapped_send
324329
)
325330
except Exception as exc:
326-
self._capture_request_exception(exc)
327-
raise exc from None
331+
exc_info = sys.exc_info()
332+
with capture_internal_exceptions():
333+
self._capture_request_exception(exc)
334+
reraise(*exc_info)
328335
finally:
329336
_asgi_middleware_applied.set(False)
330337

sentry_sdk/integrations/asyncio.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ async def _task_with_sentry_span_creation() -> "Any":
7979
try:
8080
result = await coro
8181
except StopAsyncIteration as e:
82-
raise e from None
82+
raise
8383
except Exception:
8484
reraise(*_capture_exception())
8585

0 commit comments

Comments
 (0)