Preserve `json.JSONDecodeError` information when handling invalid JSON in request body, to support custom exception handlers that use its information (#4057)

Co-authored-by: Sebastián Ramírez <tiangolo@gmail.com>
Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
This commit is contained in:
Sidharth Ajithkumar 2022-08-25 15:25:53 +05:30 committed by GitHub
parent afaa0391a5
commit 9359a8d65f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 3 additions and 1 deletions

View File

@ -209,7 +209,9 @@ def get_request_handler(
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"