Skip to content

Commit f66bcd6

Browse files
wrap in try finally
1 parent ddc6543 commit f66bcd6

File tree

1 file changed

+27
-26
lines changed

1 file changed

+27
-26
lines changed

sentry_sdk/integrations/fastapi.py

Lines changed: 27 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -112,32 +112,33 @@ async def _sentry_app(*args, **kwargs):
112112
sentry_scope = sentry_sdk.get_isolation_scope()
113113
sentry_scope._name = FastApiIntegration.identifier
114114

115-
response = await old_app(*args, **kwargs)
116-
117-
extractor = StarletteRequestExtractor(request)
118-
info = await extractor.extract_request_info()
119-
120-
def _make_request_event_processor(req, integration):
121-
# type: (Any, Any) -> Callable[[Event, Dict[str, Any]], Event]
122-
def event_processor(event, hint):
123-
# type: (Event, Dict[str, Any]) -> Event
124-
125-
# Extract information from request
126-
request_info = event.get("request", {})
127-
if info:
128-
if "cookies" in info and should_send_default_pii():
129-
request_info["cookies"] = info["cookies"]
130-
if "data" in info:
131-
request_info["data"] = info["data"]
132-
event["request"] = deepcopy(request_info)
133-
134-
return event
135-
136-
return event_processor
137-
138-
sentry_scope.add_event_processor(
139-
_make_request_event_processor(request, integration)
140-
)
115+
try:
116+
response = await old_app(*args, **kwargs)
117+
finally:
118+
extractor = StarletteRequestExtractor(request)
119+
info = await extractor.extract_request_info()
120+
121+
def _make_request_event_processor(req, integration):
122+
# type: (Any, Any) -> Callable[[Event, Dict[str, Any]], Event]
123+
def event_processor(event, hint):
124+
# type: (Event, Dict[str, Any]) -> Event
125+
126+
# Extract information from request
127+
request_info = event.get("request", {})
128+
if info:
129+
if "cookies" in info and should_send_default_pii():
130+
request_info["cookies"] = info["cookies"]
131+
if "data" in info:
132+
request_info["data"] = info["data"]
133+
event["request"] = deepcopy(request_info)
134+
135+
return event
136+
137+
return event_processor
138+
139+
sentry_scope.add_event_processor(
140+
_make_request_event_processor(request, integration)
141+
)
141142

142143
return response
143144

0 commit comments

Comments
 (0)