Skip to content

Commit ade10e4

Browse files
authored
Capture Sentry errors handled by integer status (#448)
Refs #439
1 parent e19a471 commit ade10e4

2 files changed

Lines changed: 51 additions & 2 deletions

File tree

‎lite_bootstrap/helpers/starlette_sentry.py‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,9 @@ def _report_handled_server_errors(application: "ASGIApp") -> None:
136136
handlers: typing.Final = inner._exception_handlers # noqa: SLF001
137137
for exception_class, handler in handlers.items():
138138
handlers[exception_class] = _reporting_server_errors(handler)
139+
status_handlers: typing.Final = inner._status_handlers # noqa: SLF001
140+
for status_code, handler in status_handlers.items():
141+
status_handlers[status_code] = _reporting_server_errors(handler)
139142

140143

141144
def _find_route_path(scope: "Scope", routes: "typing.Sequence[BaseRoute]") -> str | None:

‎tests/test_starlette_sentry_capture.py‎

Lines changed: 48 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
import uvicorn
1616
from fastmcp import FastMCP
1717
from sentry_sdk import integrations as sentry_integrations
18+
from sentry_sdk.integrations.dedupe import DedupeIntegration
1819
from sentry_sdk.integrations.fastapi import FastApiIntegration
1920
from sentry_sdk.integrations.httpx import HttpxIntegration
2021
from sentry_sdk.integrations.starlette import StarletteIntegration
@@ -453,8 +454,32 @@ def test_lean_capture_skips_client_http_exceptions(sentry_mock: SentryTestTransp
453454
assert _error_events(sentry_mock) == []
454455

455456

456-
def test_lean_capture_reports_server_http_exceptions_as_handled(sentry_mock: SentryTestTransport) -> None:
457-
assert _request(sentry_mock, "/http-503") == [status.HTTP_503_SERVICE_UNAVAILABLE]
457+
def _respond_503_sync(_: Request, __: Exception) -> Response:
458+
return Response("custom", status_code=status.HTTP_503_SERVICE_UNAVAILABLE)
459+
460+
461+
@pytest.mark.parametrize(
462+
("handler", "expected_body"),
463+
[
464+
pytest.param(None, None, id="default"),
465+
pytest.param(_respond_503, "", id="async"),
466+
pytest.param(_respond_503_sync, "custom", id="sync"),
467+
],
468+
)
469+
def test_lean_capture_reports_server_http_exceptions_as_handled(
470+
sentry_mock: SentryTestTransport,
471+
handler: typing.Callable[[Request, Exception], Response | typing.Awaitable[Response]] | None,
472+
expected_body: str | None,
473+
) -> None:
474+
application = _bootstrap(sentry_mock, sentry_additional_params={"disabled_integrations": [DedupeIntegration]})
475+
if handler is not None:
476+
application.add_exception_handler(status.HTTP_503_SERVICE_UNAVAILABLE, handler)
477+
478+
with TestClient(application) as client:
479+
response = client.get("/http-503")
480+
assert response.status_code == status.HTTP_503_SERVICE_UNAVAILABLE
481+
if expected_body is not None:
482+
assert response.text == expected_body
458483

459484
[event] = _error_events(sentry_mock)
460485
exception = event["exception"]["values"][-1]
@@ -473,6 +498,27 @@ def test_lean_capture_reports_a_handled_exception_by_its_status_code_not_the_res
473498
assert _exception_types(sentry_mock) == ["CarriesUnavailableStatusError"]
474499

475500

501+
def test_lean_capture_skips_client_status_handlers_and_explicit_server_error_responses(
502+
sentry_mock: SentryTestTransport,
503+
) -> None:
504+
application = _bootstrap(sentry_mock)
505+
application.add_exception_handler(
506+
status.HTTP_404_NOT_FOUND, functools.partial(_respond_with, status.HTTP_404_NOT_FOUND)
507+
)
508+
509+
@application.get("/explicit-503")
510+
async def explicit_503() -> Response:
511+
return Response(status_code=status.HTTP_503_SERVICE_UNAVAILABLE)
512+
513+
with TestClient(application) as client:
514+
response = client.get("/http-404")
515+
assert response.status_code == status.HTTP_404_NOT_FOUND
516+
assert response.text == ""
517+
assert client.get("/explicit-503").status_code == status.HTTP_503_SERVICE_UNAVAILABLE
518+
519+
assert _error_events(sentry_mock) == []
520+
521+
476522
def test_lean_capture_reports_handled_exceptions_of_a_handler_added_after_bootstrap(
477523
sentry_mock: SentryTestTransport,
478524
) -> None:

0 commit comments

Comments
 (0)