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
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.
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.
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.
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.
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.
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:
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.
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.
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).
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:
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.
A handler can be registered for a status code, like 404, or for an exception class.
But it seems like if I wrote a:
classMyUnprocessableEntity(HTTPException):
code=422description="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.
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.
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)defmy_error_handler(error):
assertisinstance(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.)
Activity
sgunturu5 commentedon May 14, 2018
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.
ThiefMaster commentedon May 14, 2018
Is this really the behavior we want? I think an error handle for
Exceptionshould be catchall - but in anyHTTPExceptionerror handler getting an exception that is not a subclass ofHTTPExceptionseems wrong/confusing.davidism commentedon May 14, 2018
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 theHTTPException). That seems correct, but means that they need to not make assumptions about the incoming error.ThiefMaster commentedon May 14, 2018
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.
taion commentedon Nov 1, 2018
@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:
flask/tests/test_user_error_handler.py
Lines 29 to 31 in 7e714bd
In other words, while receiving a
KeyErroris very strange if I registered a handler forInternalServerError, it's not at all strange if I registered a handler for500. However, the current handler map implementation represents error handlers registered for500identically with those registered forInternalServerError(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
500gets the "real" exception, while the handler forInternalServerErrorgets theInternalServerError), 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 isNone, though.There's an interim not-really-breaking solution where we treat the exception class as
Nonewhen 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_specis actually part of the public API.davidism commentedon Nov 1, 2018
Why do you think we should be treating exception classes differently than codes? They are the same.
This is correct.
taion commentedon Nov 1, 2018
If I install an error handler on
HTTPException, I don't expect to receive errors that are not instances ofHTTPException. Likewise even withInternalServerError, 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 on500receive the "real" exception, rather than anInternalServerError:flask/tests/test_user_error_handler.py
Line 43 in 7e714bd
davidism commentedon Nov 1, 2018
"500" is just a shortcut for "InternalServerError"
taion commentedon Nov 1, 2018
Is your view that the linked test case above is wrong, or is it that error handlers on
HTTPExceptionneed to handle non-HTTPExceptioninstances?taion commentedon Nov 1, 2018
It's also unintuitive that an error handler attached for e.g. the status code
422will not catch exceptions that are instances of classes that extendHTTPExceptionbut notUnprocessableEntity.From the docs http://flask.pocoo.org/docs/1.0/patterns/errorpages/#error-handlers:
But it seems like if I wrote a:
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.
davidism commentedon Nov 1, 2018
That test still passes if you change
@app.errorhandler(500)to@app.errorhandler(InteralServerError).davidism commentedon Nov 1, 2018
If you registered that custom exception with Werkzeug's exception map, it would work.
taion commentedon Nov 1, 2018
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 ofInternalServerError.taion commentedon Nov 1, 2018
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:
My expectation is that the
assertshould always trivially pass, and that my error handler will work. This is not currently the case, becauseerrorcan 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 on500, 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.)
RawPlutonium commentedon Feb 6, 2019
miguelgrinberg commentedon Feb 6, 2019
RawPlutonium commentedon Feb 6, 2019
miguelgrinberg commentedon Feb 6, 2019
RawPlutonium commentedon Feb 6, 2019
davidism commentedon Feb 6, 2019
RawPlutonium commentedon Feb 6, 2019