Browse Source

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 <[email protected]>
Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
pull/5307/head
Sidharth Ajithkumar 3 years ago
committed by GitHub
parent
commit
9359a8d65f
No known key found for this signature in database GPG Key ID: 4AEE18F83AFDEB23
  1. 4
      fastapi/routing.py

4
fastapi/routing.py

@ -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"

Loading…
Cancel
Save