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

Add 400 response on MultiParser exceptions #1593

Closed
wants to merge 2 commits into from

Conversation

Kludex
Copy link
Member

@Kludex Kludex commented Apr 18, 2022

Let's call this solution 1.

Why I don't like solution 1:

  • It adds a new exception to the ExceptionMiddleware, which is weird.

Possible solution 2:

  • Use HTTPException with import inside the MultiPartParser to avoid circular import.

Possible solution 3:

  • Create an http_exception.py file to hold only HTTPException, and import it on exceptions.py and formparsers.py.
  • This solution avoids the antipatterns from previous solutions.

I'm exposing this draft as a self note. Solution 3 looks like the best one.

@Kludex Kludex added this to the Version 0.20.0 milestone Apr 21, 2022
@Kludex Kludex mentioned this pull request Apr 22, 2022
2 tasks
@Kludex
Copy link
Member Author

Kludex commented Apr 22, 2022

Solution 2 and 3 are problematic because we need the information that we are running on a Starlette application or not (so we either raise HTTPException or return a PlainTextResponse), and we can't retrieve that there. 😞 See #1601 for code example.

Possible solution 4:

  • Add an exception handler on the application level to MultiParserException.

Solution 4 and 1 are similar...

EDIT: And now I realize that solution 4 and 1 have the same problem as solution 2 and 3. 🥲
EDIT2: Based on this information, I think we need to raise a clear exception on formparser i.e. except TypeError and raise a custom error e.g. MultiPartException with a clear message, and catch that exception on Starlette itself to return a 400. This will have the following behavior:

  • On app = Request(scope, receive, send); await request.form() usage, it will raise MultiPartException.
  • On app = Starlette() and further usage of .form(), it will return a response with 400.

@Kludex Kludex added bug Something isn't working clean up Refinement to existing functionality labels Apr 24, 2022
@Kludex Kludex modified the milestones: Version 0.20.0, Version 0.21.0 Apr 24, 2022
@Kludex
Copy link
Member Author

Kludex commented May 3, 2022

Close this in favor of #1617, but the monologue here is important for that one.

@Kludex Kludex closed this May 3, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working clean up Refinement to existing functionality
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Send 400 on missing boundary multipart/form-data subpart without name causes internal error
1 participant