From ca0209dfc2406d794d1f97595f75cfbf1b31be60 Mon Sep 17 00:00:00 2001 From: Mathieu Leplatre Date: Tue, 26 Aug 2025 17:36:12 +0200 Subject: [PATCH] Log request body on 422 responses --- jbi/app.py | 8 ++++++++ tests/unit/test_app.py | 1 + 2 files changed, 9 insertions(+) diff --git a/jbi/app.py b/jbi/app.py index 59ad0904..f6a67839 100644 --- a/jbi/app.py +++ b/jbi/app.py @@ -128,11 +128,19 @@ async def validation_exception_handler( errors in order to log some information about malformed requests. """ + try: + request_body = (await request.body()).decode("utf8") + except UnicodeDecodeError: # pragma: no cover + # In theory FastAPI would have returned a 400 before doing + # model validation. + request_body = "" + logger.error( "invalid incoming request: %s", exc, extra={ "errors": exc.errors(), + "body": request_body, }, ) return JSONResponse( diff --git a/tests/unit/test_app.py b/tests/unit/test_app.py index 4e188fca..6bf8a4f5 100644 --- a/tests/unit/test_app.py +++ b/tests/unit/test_app.py @@ -51,6 +51,7 @@ def test_422_errors_are_logged(authenticated_client, webhook_request_factory, ca logged.errors[0]["msg"] == "Input should be a valid dictionary or object to extract fields from" ) + assert '"bug":null' in logged.body @pytest.mark.parametrize(