Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Preserve json.JSONDecodeError when handling invalid JSON in request body #3993

Closed
9 tasks done
declaresub opened this issue Oct 5, 2021 · 5 comments
Closed
9 tasks done
Labels
answered feature New feature or request reviewed

Comments

@declaresub
Copy link

First Check

  • I added a very descriptive title to this issue.
  • I used the GitHub search to find a similar issue and didn't find it.
  • I searched the FastAPI documentation, with the integrated search.
  • I already searched in Google "How to X in FastAPI" and didn't find any information.
  • I already read and followed all the tutorial in the docs and didn't find an answer.
  • I already checked if it is not related to FastAPI but to Pydantic.
  • I already checked if it is not related to FastAPI but to Swagger UI.
  • I already checked if it is not related to FastAPI but to ReDoc.

Commit to Help

  • I commit to help with one of those options 👆

Example Code

not applicable

Description

I think that the response to a request with syntactically invalid JSON should be 400, not 422. This is the feature request I would prefer. But I would be willing to write a custom exception handler for now, if that were feasible.

Wanted Solution

In FastAPI/routing.py line 210-211, we have

    except json.JSONDecodeError as e:
        raise RequestValidationError([ErrorWrapper(e, ("body", e.pos))], body=e.doc)

I propose that the original exception not be discarded, by raising from --

    except json.JSONDecodeError as e:
        raise RequestValidationError([ErrorWrapper(e, ("body", e.pos))], body=e.doc) from e

With the original exception included, it would be simple enough to write a custom exception handler to handle the request as I see fit. This change should not break anyone's code, and I think it is generally the better practice to use raise from in such situations.

Wanted Code

not applicable

Alternatives

No response

Operating System

Linux, macOS

Operating System Details

No response

FastAPI Version

0.63.0

Python Version

3.9.5

Additional Context

No response

@declaresub declaresub added the feature New feature or request label Oct 5, 2021
@UKnowWhoIm
Copy link
Contributor

Is this currently being worked on @declaresub ?

@declaresub
Copy link
Author

I don't know. I would be happy to do it, but this is the first issue I've submitted to fastapi, and with 389 open pull requests, I didn't see the point spending time on a pull request that would never be looked at.

@tiangolo
Copy link
Owner

Thanks for posting it @declaresub! And thanks @UKnowWhoIm for the PR! 🎉 🍰

This should be solved in #4057 and will be available in the next version, released in some hours or the next days. (the current latest is 0.80.0).

@github-actions
Copy link
Contributor

github-actions bot commented Sep 5, 2022

Assuming the original need was handled, this will be automatically closed now. But feel free to add more comments or create new issues or PRs.

@Quix0r
Copy link

Quix0r commented Jun 4, 2023

I know this is an old ticket, but isn't it json.decoder.JSONDecodeError instead? E.g. starlette uses the long version while this here the short version (lacking .decoder package in import reference).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
answered feature New feature or request reviewed
Projects
None yet
Development

No branches or pull requests

4 participants