1515import uvicorn
1616from fastmcp import FastMCP
1717from sentry_sdk import integrations as sentry_integrations
18+ from sentry_sdk .integrations .dedupe import DedupeIntegration
1819from sentry_sdk .integrations .fastapi import FastApiIntegration
1920from sentry_sdk .integrations .httpx import HttpxIntegration
2021from 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+
476522def test_lean_capture_reports_handled_exceptions_of_a_handler_added_after_bootstrap (
477523 sentry_mock : SentryTestTransport ,
478524) -> None :
0 commit comments