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

Centralize error handling in ExceptionMiddleware #1754

Merged
merged 4 commits into from
Oct 29, 2023

Conversation

RobbeSneyders
Copy link
Member

I was writing the documentation on exception handling, and I noticed that it was very hard to explain our current behavior.

Error handlers can be registered either on the internal Flask app (not the Starlette one) or on the Connexion app, which leads to some undefined (actually just really hard to explain) behavior. Eg.

  • Registering error handlers on a status code would capture starlette.HTTPException errors on the Connexion app, and werkzeug.HTTPException errors on the Flask App, which means that registering an error handler on a status code doesn't catch all the errors with that status code.
  • Flask does some default error handling which leads to some exceptions never reaching the error handlers registered on the Connexion app.

So I made the following changes:

  • Replaced the default error handlers we registered on the Flask app with a default handler on the ExceptionMiddleware that takes into account other handlers registered on status codes.
  • Configured Flask to propagate exceptions instead of catching them.
  • Abstracted away the Starlette Request and Response types, so users can and must now use ConnexionRequest
    and ConnexionResponse types in error handlers.
  • Renamed the ASGIRequest class to ConnexionRequest since it is the only Request class part of the high level
    Connexion interface.

We could also rename ConnexionRequest and ConnexionResponse to just Request and Response. Wdyt?

@coveralls
Copy link

Coverage Status

coverage: 94.409% (-0.04%) from 94.453% when pulling 02806e3 on feature/propagate-flask-exceptions into 1b72019 on main.

@RobbeSneyders RobbeSneyders merged commit b9ba13c into main Oct 29, 2023
7 of 8 checks passed
@RobbeSneyders RobbeSneyders deleted the feature/propagate-flask-exceptions branch October 29, 2023 08:37
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants