Skip to content

Commit e4f7feb

Browse files
authored
The Falcon integration for Falcon 3.0.1 (#1297)
Update to the Falcon integration to support Falcon 3.0.1
1 parent b6d8131 commit e4f7feb

1 file changed

Lines changed: 42 additions & 16 deletions

File tree

sentry_sdk/integrations/falcon.py

Lines changed: 42 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,21 @@
2828
raise DidNotEnable("Falcon not installed")
2929

3030

31+
# In Falcon 3.0 `falcon.api_helpers` is renamed to `falcon.app_helpers`
32+
# and `falcon.API` to `falcon.App`
33+
try:
34+
import falcon.app # type: ignore
35+
import falcon.app_helpers # type: ignore
36+
37+
falcon_helpers = falcon.app_helpers
38+
falcon_app_class = falcon.App
39+
FALCON3 = True
40+
except ImportError:
41+
falcon_helpers = falcon.api_helpers
42+
falcon_app_class = falcon.API
43+
FALCON3 = False
44+
45+
3146
class FalconRequestExtractor(RequestExtractor):
3247
def env(self):
3348
# type: () -> Dict[str, Any]
@@ -58,16 +73,27 @@ def raw_data(self):
5873
else:
5974
return None
6075

61-
def json(self):
62-
# type: () -> Optional[Dict[str, Any]]
63-
try:
64-
return self.request.media
65-
except falcon.errors.HTTPBadRequest:
66-
# NOTE(jmagnusson): We return `falcon.Request._media` here because
67-
# falcon 1.4 doesn't do proper type checking in
68-
# `falcon.Request.media`. This has been fixed in 2.0.
69-
# Relevant code: https://github.com/falconry/falcon/blob/1.4.1/falcon/request.py#L953
70-
return self.request._media
76+
if FALCON3:
77+
78+
def json(self):
79+
# type: () -> Optional[Dict[str, Any]]
80+
try:
81+
return self.request.media
82+
except falcon.errors.HTTPBadRequest:
83+
return None
84+
85+
else:
86+
87+
def json(self):
88+
# type: () -> Optional[Dict[str, Any]]
89+
try:
90+
return self.request.media
91+
except falcon.errors.HTTPBadRequest:
92+
# NOTE(jmagnusson): We return `falcon.Request._media` here because
93+
# falcon 1.4 doesn't do proper type checking in
94+
# `falcon.Request.media`. This has been fixed in 2.0.
95+
# Relevant code: https://github.com/falconry/falcon/blob/1.4.1/falcon/request.py#L953
96+
return self.request._media
7197

7298

7399
class SentryFalconMiddleware(object):
@@ -120,7 +146,7 @@ def setup_once():
120146

121147
def _patch_wsgi_app():
122148
# type: () -> None
123-
original_wsgi_app = falcon.API.__call__
149+
original_wsgi_app = falcon_app_class.__call__
124150

125151
def sentry_patched_wsgi_app(self, env, start_response):
126152
# type: (falcon.API, Any, Any) -> Any
@@ -135,12 +161,12 @@ def sentry_patched_wsgi_app(self, env, start_response):
135161

136162
return sentry_wrapped(env, start_response)
137163

138-
falcon.API.__call__ = sentry_patched_wsgi_app
164+
falcon_app_class.__call__ = sentry_patched_wsgi_app
139165

140166

141167
def _patch_handle_exception():
142168
# type: () -> None
143-
original_handle_exception = falcon.API._handle_exception
169+
original_handle_exception = falcon_app_class._handle_exception
144170

145171
def sentry_patched_handle_exception(self, *args):
146172
# type: (falcon.API, *Any) -> Any
@@ -170,12 +196,12 @@ def sentry_patched_handle_exception(self, *args):
170196

171197
return was_handled
172198

173-
falcon.API._handle_exception = sentry_patched_handle_exception
199+
falcon_app_class._handle_exception = sentry_patched_handle_exception
174200

175201

176202
def _patch_prepare_middleware():
177203
# type: () -> None
178-
original_prepare_middleware = falcon.api_helpers.prepare_middleware
204+
original_prepare_middleware = falcon_helpers.prepare_middleware
179205

180206
def sentry_patched_prepare_middleware(
181207
middleware=None, independent_middleware=False
@@ -187,7 +213,7 @@ def sentry_patched_prepare_middleware(
187213
middleware = [SentryFalconMiddleware()] + (middleware or [])
188214
return original_prepare_middleware(middleware, independent_middleware)
189215

190-
falcon.api_helpers.prepare_middleware = sentry_patched_prepare_middleware
216+
falcon_helpers.prepare_middleware = sentry_patched_prepare_middleware
191217

192218

193219
def _exception_leads_to_http_5xx(ex):

0 commit comments

Comments
 (0)