Skip to content

Commit 3ebeee8

Browse files
committed
feat: Allow integrations to define control flow exceptions
1 parent bb3fcb4 commit 3ebeee8

2 files changed

Lines changed: 15 additions & 9 deletions

File tree

sentry_sdk/integrations/aiohttp.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@
4848
logger,
4949
parse_url,
5050
parse_version,
51+
register_control_flow_exception,
5152
reraise,
5253
transaction_from_function,
5354
)
@@ -103,6 +104,11 @@ def setup_once() -> None:
103104
version = parse_version(AIOHTTP_VERSION)
104105
_check_minimum_version(AioHttpIntegration, version)
105106

107+
# In the aiohttp integration, all of their HTTP responses are Exceptions.
108+
# Because they have to be raised and handled by the framework, we need this check so
109+
# that we don't accidentally overwrite a status of "ok" with "error".
110+
register_control_flow_exception(HTTPException)
111+
106112
if not HAS_REAL_CONTEXTVARS:
107113
# We better have contextvars or we're going to leak state between
108114
# requests.

sentry_sdk/utils.py

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -27,11 +27,6 @@
2727
# Python 3.10 and below
2828
BaseExceptionGroup = None # type: ignore
2929

30-
try:
31-
from aiohttp.web_exceptions import HTTPException as AIOHttpHttpException
32-
except ImportError:
33-
AIOHttpHttpException = None
34-
3530
from typing import TYPE_CHECKING
3631

3732
import sentry_sdk
@@ -93,6 +88,10 @@
9388
"is_sentry_internal_task", default=False
9489
)
9590

91+
# These exceptions won't set the span status to error if they occur. Use
92+
# register_control_flow_exception to add to this list
93+
_control_flow_exceptions = []
94+
9695

9796
def is_internal_task() -> bool:
9897
return _is_sentry_internal_task.get()
@@ -1983,15 +1982,16 @@ def get_current_thread_meta(
19831982
return None, None
19841983

19851984

1985+
def register_control_flow_exception(exc_type: type) -> None:
1986+
_control_flow_exceptions.append(exc_type)
1987+
1988+
19861989
def should_be_treated_as_error(ty: "Any", value: "Any") -> bool:
19871990
if ty == SystemExit and hasattr(value, "code") and value.code in (0, None):
19881991
# https://docs.python.org/3/library/exceptions.html#SystemExit
19891992
return False
19901993

1991-
# In the aiohttp integration, all of their HTTP responses are Exceptions.
1992-
# Because they have to be raised and handled by the framework, we need this check so
1993-
# that we don't accidentally overwrite a status of "ok" with "error" here.
1994-
if AIOHttpHttpException and isinstance(value, AIOHttpHttpException):
1994+
if ty in _control_flow_exceptions:
19951995
return False
19961996

19971997
return True

0 commit comments

Comments
 (0)