HTTPException handler catches non-error exceptions like RequestRedirect
See original GitHub issueThis is related to https://github.com/pallets/flask/issues/2841, but the emphasis is different.
Expected Behavior
Non-error exceptions should not be caught by registered error handlers.
app.register_error_handler(HTTPException, my_error_handler)
@app.route('/path-with-slash/')
def my_route():
return '', 204
Attempting to visit /path-with-slash
should redirect to /path-with-slash/
, without invoking my_error_handler
.
Actual Behavior
As the RequestRedirect
raised internally in Werkzeug is a subclass of HTTPException
, my_error_handler
will get called. This is unexpected because no error has occurred, and the only appropriate way to handle this case is to add boilerplate to re-raise any Werkzeug RoutingException
s.
Environment
- Python version: 3.7.0
- Flask version: 1.0.2
- Werkzeug version: 0.14.1
Issue Analytics
- State:
- Created 5 years ago
- Comments:14 (14 by maintainers)
Top Results From Across the Web
Handling Errors - FastAPI
When raising an HTTPException , you can pass any value that can be converted to JSON as the parameter detail , not only...
Read more >Handling Application Errors — Flask Documentation (2.2.x)
werkzeug.exceptions.HTTPException subclasses like BadRequest and their HTTP codes are interchangeable when registering handlers. ( BadRequest.code == 400 ).
Read more >Global error handler for any exception - python - Stack Overflow
You can use @app.errorhandler(Exception) : Demo (the HTTPException check ensures that the status code is preserved): from flask import Flask, abort, ...
Read more >Everything you wanted to know about exceptions - PowerShell
When an exception is thrown, that call stack is checked in order for an exception handler to catch it. Terminating and non-terminating errors....
Read more >Exceptions | Sanic Framework
You can also create a catchall handler by catching Exception . ... In that case, you can subclass Sanic's default error handler as...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
I’ve added https://github.com/pallets/flask/pull/2986 as a strawman to demonstrate what the fix would look like, and updated the test to demonstrate the behavior that it enables.
I disagree with that statement.
For example, returning
flask.redirect
does not in general raise aRequestRedirect
, so one cannot use error handlers onRequestRedirect
to implement custom request behavior in the first place.If one wishes to customize Werkzeug’s routing logic, the correct way to handle that would be to do so in the router, rather than by using what should be the app’s error handling logic.