2323
2424try :
2525 import falcon # type: ignore
26+ from falcon import App , app_helpers
2627 from falcon import __version__ as FALCON_VERSION
28+ from falcon .request import _UNSET as _FALCON_UNSET # type: ignore
2729except ImportError :
2830 raise DidNotEnable ("Falcon not installed" )
2931
30- try :
31- import falcon .app_helpers # type: ignore
32-
33- falcon_helpers = falcon .app_helpers
34- falcon_app_class = falcon .App
35- FALCON3 = True
36- except ImportError :
37- import falcon .api_helpers # type: ignore
38-
39- falcon_helpers = falcon .api_helpers
40- falcon_app_class = falcon .API
41- FALCON3 = False
42-
43-
44- _FALCON_UNSET : "Optional[object]" = None
45- if FALCON3 : # falcon.request._UNSET is only available in Falcon 3.0+
46- with capture_internal_exceptions ():
47- from falcon .request import ( # type: ignore[import-not-found, no-redef]
48- _UNSET as _FALCON_UNSET ,
49- )
50-
5132
5233class FalconRequestExtractor (RequestExtractor ):
5334 def env (self ) -> "Dict[str, Any]" :
@@ -137,23 +118,22 @@ class FalconIntegration(Integration):
137118 def __init__ (self , transaction_style : str = "uri_template" ) -> None :
138119 if transaction_style not in TRANSACTION_STYLE_VALUES :
139120 raise ValueError (
140- "Invalid value for transaction_style: %s (must be in %s) "
141- % ( transaction_style , TRANSACTION_STYLE_VALUES )
121+ f "Invalid value for transaction_style: { transaction_style } "
122+ f"(must be in { TRANSACTION_STYLE_VALUES } )"
142123 )
143124 self .transaction_style = transaction_style
144125
145126 @staticmethod
146127 def setup_once () -> None :
147- version = parse_version (FALCON_VERSION )
148- _check_minimum_version (FalconIntegration , version )
128+ _check_minimum_version (FalconIntegration , parse_version (FALCON_VERSION ))
149129
150130 _patch_wsgi_app ()
151131 _patch_handle_exception ()
152132 _patch_prepare_middleware ()
153133
154134
155135def _patch_wsgi_app () -> None :
156- original_wsgi_app = falcon_app_class .__call__
136+ original_wsgi_app = App .__call__
157137
158138 def sentry_patched_wsgi_app (
159139 self : "falcon.API" , env : "Any" , start_response : "Any"
@@ -169,11 +149,11 @@ def sentry_patched_wsgi_app(
169149
170150 return sentry_wrapped (env , start_response )
171151
172- falcon_app_class .__call__ = sentry_patched_wsgi_app
152+ App .__call__ = sentry_patched_wsgi_app
173153
174154
175155def _patch_handle_exception () -> None :
176- original_handle_exception = falcon_app_class ._handle_exception
156+ original_handle_exception = App ._handle_exception
177157
178158 @ensure_integration_enabled (FalconIntegration , original_handle_exception )
179159 def sentry_patched_handle_exception (self : "falcon.API" , * args : "Any" ) -> "Any" :
@@ -205,11 +185,11 @@ def sentry_patched_handle_exception(self: "falcon.API", *args: "Any") -> "Any":
205185
206186 return was_handled
207187
208- falcon_app_class ._handle_exception = sentry_patched_handle_exception
188+ App ._handle_exception = sentry_patched_handle_exception
209189
210190
211191def _patch_prepare_middleware () -> None :
212- original_prepare_middleware = falcon_helpers .prepare_middleware
192+ original_prepare_middleware = app_helpers .prepare_middleware
213193
214194 def sentry_patched_prepare_middleware (
215195 middleware : "Any" = None ,
@@ -224,11 +204,9 @@ def sentry_patched_prepare_middleware(
224204 if integration is not None :
225205 middleware = [SentryFalconMiddleware ()] + (middleware or [])
226206
227- # We intentionally omit the asgi argument here, since the default is False anyways,
228- # and this way, we remain backwards-compatible with pre-3.0.0 Falcon versions.
229- return original_prepare_middleware (middleware , independent_middleware )
207+ return original_prepare_middleware (middleware , independent_middleware , asgi )
230208
231- falcon_helpers .prepare_middleware = sentry_patched_prepare_middleware
209+ app_helpers .prepare_middleware = sentry_patched_prepare_middleware
232210
233211
234212def _exception_leads_to_http_5xx (ex : Exception , response : "falcon.Response" ) -> bool :
@@ -239,14 +217,7 @@ def _exception_leads_to_http_5xx(ex: Exception, response: "falcon.Response") ->
239217 ex , (falcon .HTTPError , falcon .http_status .HTTPStatus )
240218 )
241219
242- # We only check the HTTP status on Falcon 3 because in Falcon 2, the status on the response
243- # at the stage where we capture it is listed as 200, even though we would expect to see a 500
244- # status. Since at the time of this change, Falcon 2 is ca. 4 years old, we have decided to
245- # only perform this check on Falcon 3+, despite the risk that some handled errors might be
246- # reported to Sentry as unhandled on Falcon 2.
247- return (is_server_error or is_unhandled_error ) and (
248- not FALCON3 or _has_http_5xx_status (response )
249- )
220+ return (is_server_error or is_unhandled_error ) and _has_http_5xx_status (response )
250221
251222
252223def _has_http_5xx_status (response : "falcon.Response" ) -> bool :
0 commit comments