Skip to content

Registered error handler (using register_error_handler) processes inappropriate exceptions #2778

Closed

Description

@woxcab

If register an error handler for exception class A then this error handler will be called for any exception B that isn't A or subclass of A.

Expected Behavior

Given error handler must be called only for exceptions that's instance of A or instance of subclass of A.

Actual Behavior

Error handler processes any exception.

How to reproduce

Minimal code:

import flask
from werkzeug.exceptions import HTTPException

app = flask.Flask(__name__)
app.register_error_handler(HTTPException, lambda e: (str(e), e.code))

@app.route('/')
def index():
    raise ValueError()

app.run()
  1. Run script
  2. Go to http://127.0.0.1:5000/

Server logs exception AttributeError: 'ValueError' object has no attribute 'code', i.e. registered error handler is called for ValueError exception, but the error handler is registered only for HTTPException and its subclasses.

Environment

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

Activity

  1. sgunturu5 commented on May 14, 2018

    @sgunturu5

    Discussed with @davidism
    HTTPException Handler and InternalServerError is the default exception handle for unhandled exceptions. Hence it need to be generic. Planning to update the documentation.

  2. ThiefMaster commented on May 14, 2018

    @ThiefMaster
    Member

    Is this really the behavior we want? I think an error handle for Exception should be catchall - but in any HTTPException error handler getting an exception that is not a subclass of HTTPException seems wrong/confusing.

  3. davidism commented on May 14, 2018

    @davidism
    Member

    Flask wants to raise a 500 error when there is an unhandled exception. But it tries to use the InternalServerError (500) error handler to handle that 500 error (which falls back to the HTTPException). That seems correct, but means that they need to not make assumptions about the incoming error.

  4. ThiefMaster commented on May 14, 2018

    @ThiefMaster
    Member

    What I mean is that the error handler should receive the 500 error, but with an InternalServerError exception -- not the original exception that caused it. IMO people should register an error handler for Exception, ValueError, etc. if they want these exceptions.

  5. taion commented on Nov 1, 2018

    @taion
    Contributor

    @ThiefMaster

    I spent a few minutes poking around at your recommended solution, because it was the one that seemed to make sense, and it's straightforward to implement.

    However, the test suite gives an example for when the current behavior might be desired:

    @app.errorhandler(500)
    def handle_500(e):
    return type(e).__name__

    In other words, while receiving a KeyError is very strange if I registered a handler for InternalServerError, it's not at all strange if I registered a handler for 500. However, the current handler map implementation represents error handlers registered for 500 identically with those registered for InternalServerError (as error handlers registered for a code are treated as those registered for the default exception type for that code).

    So, assuming the behavior above is desired (i.e. the handler for 500 gets the "real" exception, while the handler for InternalServerError gets the InternalServerError), the obvious implementation would be to track error handlers for status codes entirely separately from those for exception classes.

    However, this would require changing the shape of error_handler_spec, and even though this isn't something users should have any good reasons to use, it's actually part of the documented API: http://flask.pocoo.org/docs/1.0/api/?highlight=error_handler_spec#flask.Flask.error_handler_spec. It's worth noting that this documentation is actually incorrect in the case where the status is None, though.

    There's an interim not-really-breaking solution where we treat the exception class as None when registering errors on a status code. Perhaps I'll demonstrate this on a PR.

    But otherwise let me know what we think about the possibility of a larger refactor here, and whether error_handler_spec is actually part of the public API.

  6. davidism commented on Nov 1, 2018

    @davidism
    Member

    Why do you think we should be treating exception classes differently than codes? They are the same.

    However, the current handler map implementation represents error handlers registered for 500 identically with those registered for InternalServerError (as error handlers registered for a code are treated as those registered for the default exception type for that code).

    This is correct.

  7. taion commented on Nov 1, 2018

    @taion
    Contributor

    If I install an error handler on HTTPException, I don't expect to receive errors that are not instances of HTTPException. Likewise even with InternalServerError, I would argue.

    However, if I install an error handler on 500, then it's less clear. And in fact the test suite currently explicitly asserts that error handlers on 500 receive the "real" exception, rather than an InternalServerError:

    assert client.get('/keyerror').data == b'KeyError'
    .

  8. davidism commented on Nov 1, 2018

    @davidism
    Member

    "500" is just a shortcut for "InternalServerError"

  9. taion commented on Nov 1, 2018

    @taion
    Contributor

    Is your view that the linked test case above is wrong, or is it that error handlers on HTTPException need to handle non-HTTPException instances?

  10. taion commented on Nov 1, 2018

    @taion
    Contributor

    It's also unintuitive that an error handler attached for e.g. the status code 422 will not catch exceptions that are instances of classes that extend HTTPException but not UnprocessableEntity.

    From the docs http://flask.pocoo.org/docs/1.0/patterns/errorpages/#error-handlers:

    A handler can be registered for a status code, like 404, or for an exception class.

    But it seems like if I wrote a:

    class MyUnprocessableEntity(HTTPException):
        code = 422
        description = "Some other description of an unprocessable entity error."

    Then an error handler on 422 should catch this, which suggests that (in addition to the case above with handling internal errors) there's reason to not treat status codes as identical to the default exception classes in all cases.

  11. davidism commented on Nov 1, 2018

    @davidism
    Member

    That test still passes if you change @app.errorhandler(500) to @app.errorhandler(InteralServerError).

  12. davidism commented on Nov 1, 2018

    @davidism
    Member

    If you registered that custom exception with Werkzeug's exception map, it would work.

  13. taion commented on Nov 1, 2018

    @taion
    Contributor

    That test still passes if you change @app.errorhandler(500) to @app.errorhandler(InteralServerError).

    That's exactly the problem here, though. Per @ThiefMaster in #2778 (comment), if I install an exception handler on InternalServerError, I do not except to receive exceptions that are not instances of InternalServerError.

  14. taion commented on Nov 1, 2018

    @taion
    Contributor

    I'm not disputing your explanation of the current behavior.

    The gist of this issue is that the current behavior is unexpected and seems buggy to me. Per the OP, if I do:

    @app.errorhandler(HTTPException)
    def my_error_handler(error):
        assert isinstance(error, HTTPException)
        return "I just had an error", error.code

    My expectation is that the assert should always trivially pass, and that my error handler will work. This is not currently the case, because error can be whatever internal exception gets thrown in my app.

    My PR in #2983 makes the above handler only receive instances of HTTPException, while preserving the ability to register handlers on 500, keeping the existing behavior there.

    (And actually, per #2984, it really won't work as expected, because internal routing exceptions that I do not understand as errors will also get handled by my handler – incorrectly, at that.)

  15. RawPlutonium commented on Feb 6, 2019

    @RawPlutonium
  16. miguelgrinberg commented on Feb 6, 2019

    @miguelgrinberg
  17. RawPlutonium commented on Feb 6, 2019

    @RawPlutonium
  18. miguelgrinberg commented on Feb 6, 2019

    @miguelgrinberg
  19. RawPlutonium commented on Feb 6, 2019

    @RawPlutonium
  20. davidism commented on Feb 6, 2019

    @davidism
  21. RawPlutonium commented on Feb 6, 2019

    @RawPlutonium
  22. added this to the 1.1.0 milestone on Jun 19, 2019
  23. 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

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions