Skip to content

HTTPException handler catches non-error exceptions like RequestRedirect #2984

Closed

Description

@taion

This is related to #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 RoutingExceptions.

Environment

  • Python version: 3.7.0
  • Flask version: 1.0.2
  • Werkzeug version: 0.14.1

Activity

  1. changed the title [-]Global error handler catches non-error exceptions like RequestRedirect[/-] [+]HTTPException handler catches non-error exceptions like RequestRedirect[/+] on Nov 1, 2018
  2. taion commented on Nov 1, 2018

    @taion
    ContributorAuthor

    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.

  3. davidism commented on Nov 1, 2018

    @davidism
    Member

    I'm not clear how this is different than #2841.

  4. taion commented on Nov 1, 2018

    @taion
    ContributorAuthor

    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.

  5. davidism commented on Nov 1, 2018

    @davidism
    Member

    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)

  6. taion commented on Nov 1, 2018

    @taion
    ContributorAuthor

    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.

  7. ThiefMaster commented on Nov 1, 2018

    @ThiefMaster
    Member

    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?

  8. davidism commented on Nov 1, 2018

    @davidism
    Member

    Unfortunately the name "errorhandler" is confusing. What it actually means is "exception handler". An exception is not necessarily an error, as people are discovering.

  9. davidism commented on Nov 1, 2018

    @davidism
    Member

    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.

  10. taion commented on Nov 1, 2018

    @taion
    ContributorAuthor

    These are the differences:

    1. RequestRedirect is not part of exceptions.default_exceptions
    2. 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
    3. RequestRedirect can be generally distinguished from other HTTPExceptions because it implements RoutingException, which is explicitly flagged in Werkzeug as a set of "special exceptions"
    4. RequestRedirect is not semantically an error
  11. ThiefMaster commented on Nov 1, 2018

    @ThiefMaster
    Member

    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.

  12. davidism commented on Nov 1, 2018

    @davidism
    Member

    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.

  13. davidism commented on Nov 1, 2018

    @davidism
    Member

    The problem with special casing RoutingExcpetion to skip handling is that as soon as someone does want to adjust how their redirects work, they can't.

  14. taion commented on Nov 1, 2018

    @taion
    ContributorAuthor

    I disagree with that statement.

    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.

  15. taion commented on Nov 1, 2018

    @taion
    ContributorAuthor

    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.

  16. locked as resolved and limited conversation to collaborators on Nov 14, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions