Skip to content

Commit 40da182

Browse files
authored
Log request body on 422 responses (#1183)
1 parent 46764f0 commit 40da182

File tree

2 files changed

+9
-0
lines changed

2 files changed

+9
-0
lines changed

jbi/app.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -128,11 +128,19 @@ async def validation_exception_handler(
128128
errors in order to log some information about malformed
129129
requests.
130130
"""
131+
try:
132+
request_body = (await request.body()).decode("utf8")
133+
except UnicodeDecodeError: # pragma: no cover
134+
# In theory FastAPI would have returned a 400 before doing
135+
# model validation.
136+
request_body = "<invalid unicode>"
137+
131138
logger.error(
132139
"invalid incoming request: %s",
133140
exc,
134141
extra={
135142
"errors": exc.errors(),
143+
"body": request_body,
136144
},
137145
)
138146
return JSONResponse(

tests/unit/test_app.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ def test_422_errors_are_logged(authenticated_client, webhook_request_factory, ca
5151
logged.errors[0]["msg"]
5252
== "Input should be a valid dictionary or object to extract fields from"
5353
)
54+
assert '"bug":null' in logged.body
5455

5556

5657
@pytest.mark.parametrize(

0 commit comments

Comments
 (0)