-
Notifications
You must be signed in to change notification settings - Fork 550
fix(Litestar): Apply failed_request_status_codes
to exceptions raised in middleware
#4074
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from 20 commits
0b64a40
4f1963c
169177f
3950e2a
e1cd18e
2b91e0d
6ac821b
0c2da8f
204c8a7
bace5ce
fb1274f
babfe27
33b11cc
41b5fd5
3819888
5b27acd
53baddd
c1c318d
a401a5e
524e77e
c26763f
2158823
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -402,7 +402,7 @@ async def __call__(self, scope, receive, send): | |
|
||
|
||
@parametrize_test_configurable_status_codes | ||
def test_configurable_status_codes( | ||
def test_configurable_status_codes_handler( | ||
sentry_init, | ||
capture_events, | ||
failed_request_status_codes, | ||
|
@@ -427,3 +427,64 @@ async def error() -> None: | |
client.get("/error") | ||
|
||
assert len(events) == int(expected_error) | ||
|
||
|
||
@parametrize_test_configurable_status_codes | ||
def test_configurable_status_codes_middleware( | ||
sentry_init, | ||
capture_events, | ||
failed_request_status_codes, | ||
status_code, | ||
expected_error, | ||
): | ||
integration_kwargs = ( | ||
{"failed_request_status_codes": failed_request_status_codes} | ||
if failed_request_status_codes is not None | ||
else {} | ||
) | ||
sentry_init(integrations=[LitestarIntegration(**integration_kwargs)]) | ||
|
||
events = capture_events() | ||
|
||
def create_raising_middleware(app): | ||
async def raising_middleware(scope, receive, send): | ||
raise HTTPException(status_code=status_code) | ||
|
||
return raising_middleware | ||
|
||
@get("/error") | ||
async def error() -> None: ... | ||
|
||
app = Litestar([error], middleware=[create_raising_middleware]) | ||
client = TestClient(app) | ||
client.get("/error") | ||
|
||
assert len(events) == int(expected_error) | ||
|
||
|
||
def test_catch_non_http_exceptions_in_middleware( | ||
sentry_init, | ||
capture_events, | ||
): | ||
sentry_init(integrations=[LitestarIntegration()]) | ||
|
||
events = capture_events() | ||
|
||
def create_raising_middleware(app): | ||
async def raising_middleware(scope, receive, send): | ||
raise RuntimeError("Too Hot") | ||
|
||
return raising_middleware | ||
|
||
@get("/error") | ||
async def error() -> None: ... | ||
|
||
app = Litestar([error], middleware=[create_raising_middleware]) | ||
client = TestClient(app) | ||
|
||
try: | ||
client.get("/error") | ||
except RuntimeError: | ||
pass | ||
|
||
assert len(events) == 1 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'd consider also asserting that the event is as expected (i.e. it is a runtime error with the message "Too Hot") There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Makes sense. I’ll add it tomorrow 👍 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Done! |
Uh oh!
There was an error while loading. Please reload this page.