diff --git a/sentry_sdk/integrations/falcon.py b/sentry_sdk/integrations/falcon.py index 1bb79428f1..9b3cc40cd6 100644 --- a/sentry_sdk/integrations/falcon.py +++ b/sentry_sdk/integrations/falcon.py @@ -206,13 +206,20 @@ def _patch_prepare_middleware(): original_prepare_middleware = falcon_helpers.prepare_middleware def sentry_patched_prepare_middleware( - middleware=None, independent_middleware=False + middleware=None, independent_middleware=False, asgi=False ): - # type: (Any, Any) -> Any + # type: (Any, Any, bool) -> Any + if asgi: + # We don't support ASGI Falcon apps, so we don't patch anything here + return original_prepare_middleware(middleware, independent_middleware, asgi) + hub = Hub.current integration = hub.get_integration(FalconIntegration) if integration is not None: middleware = [SentryFalconMiddleware()] + (middleware or []) + + # We intentionally omit the asgi argument here, since the default is False anyways, + # and this way, we remain backwards-compatible with pre-3.0.0 Falcon versions. return original_prepare_middleware(middleware, independent_middleware) falcon_helpers.prepare_middleware = sentry_patched_prepare_middleware diff --git a/tests/integrations/falcon/test_falcon.py b/tests/integrations/falcon/test_falcon.py index dd7aa80dfe..764b81f172 100644 --- a/tests/integrations/falcon/test_falcon.py +++ b/tests/integrations/falcon/test_falcon.py @@ -13,6 +13,14 @@ from sentry_sdk.integrations.logging import LoggingIntegration +try: + import falcon.asgi +except ImportError: + pass +else: + import falcon.inspect # We only need this module for the ASGI test + + @pytest.fixture def make_app(sentry_init): def inner(): @@ -391,3 +399,24 @@ def generator(): with sentry_sdk.configure_scope() as scope: assert not scope._tags["request_data"] + + +@pytest.mark.skipif( + not hasattr(falcon, "asgi"), reason="This Falcon version lacks ASGI support." +) +def test_falcon_not_breaking_asgi(sentry_init): + """ + This test simply verifies that the Falcon integration does not break ASGI + Falcon apps. + + The test does not verify ASGI Falcon support, since our Falcon integration + currently lacks support for ASGI Falcon apps. + """ + sentry_init(integrations=[FalconIntegration()]) + + asgi_app = falcon.asgi.App() + + try: + falcon.inspect.inspect_app(asgi_app) + except TypeError: + pytest.fail("Falcon integration causing errors in ASGI apps.")