flask: Setting error handler for unknown code fails
The following code:
flask_app.errorhandler(402)(http_exception_handler)
raises KeyError: 402 due to missing 402 in default exception. Code works fine with Flask=0.10.1
About this issue
- Original URL
- State: closed
- Created 8 years ago
- Reactions: 4
- Comments: 23 (14 by maintainers)
Commits related to this issue
- Migration to Flask 0.11... There was an issue with the error handlers which I though was a Flask problem but it was me using the wrong way of attaching the custom error handler (https://github.com/pa... — committed to pjuu/pjuu by docapotamus 8 years ago
- Mention existence of register_error_handler in errorpages.rst See https://github.com/pallets/flask/issues/1837 for context. — committed to liebald/flask by liebald 8 years ago
- Upgrade to Flask 0.11.1 Flask 0.11.1 crashes when using the obsolete app.error_handler_spec() method. Using app_register_error_handler() instead. See https://github.com/pallets/flask/issues/1837 for... — committed to pyeve/eve by nicolaiarocci 8 years ago
- Mention existence of register_error_handler in errorpages.rst See https://github.com/pallets/flask/issues/1837 for context. — committed to liebald/flask by liebald 8 years ago
Not sure this is a correct way of handling this or not but it works…
The key is that default_exceptions is a dictionary of valid HTTP errors that werkzeug processes. I suppose you could add custom HTTP codes to this dictionary as well.
The above returns a response like so:
Not a specialist of Flask code but did hit the same kind of issue, you can see gist of how we set error handling here (code has been cleaned a bit): https://gist.github.com/hrbonz/5cc9d9d1a63593cd87b3ef555470706c
This used to work fine (in 0.10.1), the problem seems to be that now (I don’t know how it was before), Flask._find_error_handler() will try to strictly match the error code from
app.error_handler_specwhen my handlers are registered under theNonekey.Should I create a factory to register every error code individually or is it a bug in how the handlers are found?
Bottom line is, it looks like we now have to register all exception codes one by one instead of simply creating a generic error handler.