You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
HTTPException handler catches non-error exceptions like RequestRedirect #2984
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 RoutingExceptions.
changed the title [-]Global error handler catches non-error exceptions like RequestRedirect[/-][+]HTTPException handler catches non-error exceptions like RequestRedirect[/+]on Nov 1, 2018
The most straightforward fix would be to add a hard-coded exception to not handle RoutingException instances with the error handling logic in Flask. RequestRedirect instances are not errors.
The difference is that the NotFound coming from routing is (1) actually an error and (2) is indistinguishable from a NotFound raised within the app. Additionally, a generic error handler that preserves the error code will still work.
None of those are true for a RequestRedirect raised internally by Werkzeug's routing logic. It's (1) not an error and (2) trivially distinguishable from other errors, as it extends RoutingException. Additionally, error handlers for generic HTTPExceptions will do something wrong here, as it's insufficient to just preserve error.code – one must preserve the Location header from the baked-in response as well, which requires reaching more deeply into the internals of the exception class.
Additionally, error handlers for generic HTTPExceptions will do something wrong here, as it's insufficient to just preserve error.code – one must preserve the Location header from the baked-in response as well, which requires reaching more deeply into the internals of the exception class.
Does my comment on the other issue not address that? A handler for the base exception should check if it's actually a bad HTTP error being handled, else return it if not. #2841 (comment)
The issue here is that no error handler should catch instances of RequestRedirect, because they're not errors. The use of RequestRedirect is an internal implementation detail of Werkzeug. RequestRedirect in fact does not even show up in exceptions.default_exceptions in Werkzeug.
This is different from e.g. NotFound, in that NotFound is an HTTP error class, and a default exception class.
could we add an option either when registering error handlers or in app.config on whether HTTPException or even Exception error handlers catch http exceptions?
Unfortunately the name "errorhandler" is confusing. What it actually means is "exception handler". An exception is not necessarily an error, as people are discovering.
I want to mark this as a duplicate. Whatever the decision in the end, I don't think there's a difference between NotFound and RequestRedirect in terms of what's causing the issue and how it can be / will be handled.
RequestRedirect is not part of exceptions.default_exceptions
One cannot normally install an error handler on status code 301; if werkzeug.exceptions added a MovedPermanently exception (with code 301), then an error handler on code 301 would, in the current implementation, not catch a RequestRedirect
RequestRedirect can be generally distinguished from other HTTPExceptions because it implements RoutingException, which is explicitly flagged in Werkzeug as a set of "special exceptions"
I don't think there's a difference between NotFound and RequestRedirect
Not sure - I think it's not too uncommon to register an error handler for http errors to show prettier error pages (github-unicorn-from-hell style ;)). But even I was a bit surprised when I got my error page for a 3xx! Ths workaround is super easy, but for someone less experienced with Flask I can imagine it's indeed pretty confusing.
Which is why it's marked as a documentation issue. We need to properly explain that errorhandler(HTTPExcpetion) is similar to except Exception in how broad it is, and show how to deal with that.
For example, returning flask.redirect does not in general raise a RequestRedirect, so one cannot use error handlers on RequestRedirect 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.
Activity
[-]Global error handler catches non-error exceptions like RequestRedirect[/-][+]HTTPException handler catches non-error exceptions like RequestRedirect[/+]taion commentedon Nov 1, 2018
The most straightforward fix would be to add a hard-coded exception to not handle
RoutingExceptioninstances with the error handling logic in Flask.RequestRedirectinstances are not errors.davidism commentedon Nov 1, 2018
I'm not clear how this is different than #2841.
taion commentedon Nov 1, 2018
The difference is that the
NotFoundcoming from routing is (1) actually an error and (2) is indistinguishable from aNotFoundraised within the app. Additionally, a generic error handler that preserves the error code will still work.None of those are true for a
RequestRedirectraised internally by Werkzeug's routing logic. It's (1) not an error and (2) trivially distinguishable from other errors, as it extendsRoutingException. Additionally, error handlers for genericHTTPExceptions will do something wrong here, as it's insufficient to just preserveerror.code– one must preserve theLocationheader from the baked-in response as well, which requires reaching more deeply into the internals of the exception class.davidism commentedon Nov 1, 2018
Does my comment on the other issue not address that? A handler for the base exception should check if it's actually a bad HTTP error being handled, else return it if not. #2841 (comment)
taion commentedon Nov 1, 2018
The issue here is that no error handler should catch instances of
RequestRedirect, because they're not errors. The use ofRequestRedirectis an internal implementation detail of Werkzeug.RequestRedirectin fact does not even show up inexceptions.default_exceptionsin Werkzeug.This is different from e.g.
NotFound, in thatNotFoundis an HTTP error class, and a default exception class.ThiefMaster commentedon Nov 1, 2018
could we add an option either when registering error handlers or in
app.configon whether HTTPException or even Exception error handlers catch http exceptions?davidism commentedon Nov 1, 2018
Unfortunately the name "errorhandler" is confusing. What it actually means is "exception handler". An exception is not necessarily an error, as people are discovering.
davidism commentedon Nov 1, 2018
I want to mark this as a duplicate. Whatever the decision in the end, I don't think there's a difference between NotFound and RequestRedirect in terms of what's causing the issue and how it can be / will be handled.
taion commentedon Nov 1, 2018
These are the differences:
RequestRedirectis not part ofexceptions.default_exceptionswerkzeug.exceptionsadded aMovedPermanentlyexception (with code 301), then an error handler on code 301 would, in the current implementation, not catch aRequestRedirectRequestRedirectcan be generally distinguished from otherHTTPExceptions because it implementsRoutingException, which is explicitly flagged in Werkzeug as a set of "special exceptions"RequestRedirectis not semantically an errorThiefMaster commentedon Nov 1, 2018
Not sure - I think it's not too uncommon to register an error handler for http errors to show prettier error pages (github-unicorn-from-hell style ;)). But even I was a bit surprised when I got my error page for a 3xx! Ths workaround is super easy, but for someone less experienced with Flask I can imagine it's indeed pretty confusing.
davidism commentedon Nov 1, 2018
Which is why it's marked as a documentation issue. We need to properly explain that
errorhandler(HTTPExcpetion)is similar toexcept Exceptionin how broad it is, and show how to deal with that.davidism commentedon Nov 1, 2018
The problem with special casing
RoutingExcpetionto skip handling is that as soon as someone does want to adjust how their redirects work, they can't.taion commentedon Nov 1, 2018
I disagree with that statement.
For example, returning
flask.redirectdoes not in general raise aRequestRedirect, so one cannot use error handlers onRequestRedirectto 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.
taion commentedon Nov 1, 2018
I've added #2986 as a strawman to demonstrate what the fix would look like, and updated the test to demonstrate the behavior that it enables.
Add version requirements for Flask to deal with redirections problem
Add version requirements for Flask to deal with redirections problem