From 9359a8d65fdff4460d01d652ccafd31507c6c426 Mon Sep 17 00:00:00 2001 From: Sidharth Ajithkumar Date: Thu, 25 Aug 2022 15:25:53 +0530 Subject: [PATCH] =?UTF-8?q?=E2=9C=A8=20Preserve=20`json.JSONDecodeError`?= =?UTF-8?q?=20information=20when=20handling=20invalid=20JSON=20in=20reques?= =?UTF-8?q?t=20body,=20to=20support=20custom=20exception=20handlers=20that?= =?UTF-8?q?=20use=20its=20information=20(#4057)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: Sebastián Ramírez Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com> --- fastapi/routing.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/fastapi/routing.py b/fastapi/routing.py index 80bd53279a31f..8f4d0fa7ef578 100644 --- a/fastapi/routing.py +++ b/fastapi/routing.py @@ -209,7 +209,9 @@ async def app(request: Request) -> Response: else: body = body_bytes except json.JSONDecodeError as e: - raise RequestValidationError([ErrorWrapper(e, ("body", e.pos))], body=e.doc) + raise RequestValidationError( + [ErrorWrapper(e, ("body", e.pos))], body=e.doc + ) from e except Exception as e: raise HTTPException( status_code=400, detail="There was an error parsing the body"