Skip to content

Commit e1fa27a

Browse files
committed
fix: Fix issue where strip extra post hook with include codes would include everything due to exclude codes defaulting to all codes.
1 parent 9e3c60f commit e1fa27a

File tree

2 files changed

+30
-1
lines changed

2 files changed

+30
-1
lines changed

src/starlette_problem/handler.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -175,7 +175,8 @@ def __init__(
175175

176176
def __call__(self: t.Self, content: dict, _request: Request, response: JSONResponse) -> tuple[dict, JSONResponse]:
177177
strip_extras = self.enabled and (
178-
response.status_code in self.include_status_codes or response.status_code not in self.exclude_status_codes
178+
response.status_code in self.include_status_codes
179+
or (not self.include_status_codes and response.status_code not in self.exclude_status_codes)
179180
)
180181

181182
new_content = content.copy()

tests/test_handler.py

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -147,6 +147,34 @@ def test_strip_extras_post_hook_exclude_status_code(self):
147147
== b'{"type":"something-wrong","title":"This is an error.","status":500,"a":"b","detail":"something bad"}'
148148
)
149149

150+
def test_strip_extras_post_hook_include_status_code(self):
151+
request = mock.Mock(headers={})
152+
exc = SomethingWrongError("something bad", a="b")
153+
154+
eh = handler.ExceptionHandler(
155+
post_hooks=[handler.StripExtrasPostHook(include_status_codes=[500], enabled=True)],
156+
)
157+
response = eh(request, exc)
158+
159+
assert (
160+
response.body
161+
== b'{"type":"something-wrong","title":"This is an error.","status":500,"detail":"something bad"}'
162+
)
163+
164+
def test_strip_extras_post_hook_include_status_code_correctly_allows_other_codes_through(self):
165+
request = mock.Mock(headers={})
166+
exc = SomethingWrongError("something bad", a="b")
167+
168+
eh = handler.ExceptionHandler(
169+
post_hooks=[handler.StripExtrasPostHook(include_status_codes=[400], enabled=True)],
170+
)
171+
response = eh(request, exc)
172+
173+
assert (
174+
response.body
175+
== b'{"type":"something-wrong","title":"This is an error.","status":500,"a":"b","detail":"something bad"}'
176+
)
177+
150178
def test_strip_extras_post_hook_custom_mandatory(self):
151179
request = mock.Mock(headers={})
152180
exc = SomethingWrongError("something bad", a="b")

0 commit comments

Comments
 (0)