flask: Registering a handler for HTTPException has no effect
When registering a handler for werkzeug.exceptions.HTTPException, it has no effect when an HTTP error is raised.
Assume the following handler:
@app.errorhandler(HTTPException)
def http_err_handler(error):
response = jsonify({
"success": False,
"message": error.name
})
response.status_code = error.code
return response
When requesting a page for which no route exists, a JSON response should be returned by the error handler, but instead, the usual Flask-generated HTTP error page is returned.
On the other hand, if the error handler is defined to handle a specific error code (by passing the error code to the app.errorhandler decorator), the exception is trapped and the JSON message returned.
As wekzeug.exceptions.HTTPException is the class raised internally by the abort() function, why isn’t it possible to create a “catch-all” handler like this? Am I missing something?
About this issue
- Original URL
- State: closed
- Created 10 years ago
- Comments: 20 (12 by maintainers)
Links to this issue
Commits related to this issue
- Fix #941 -- allow errorhandlers for HTTPException Originally i intended to rewrite the error handling system to register an error handler for each subclass of HTTPException on initialization of the F... — committed to untitaker/flask by untitaker 10 years ago
- Fix #941 -- allow errorhandlers for HTTPException Originally i intended to rewrite the error handling system to register an error handler for each subclass of HTTPException on initialization of the F... — committed to untitaker/flask by untitaker 10 years ago
- Fix #941 -- allow errorhandlers for HTTPException Originally i intended to rewrite the error handling system to register an error handler for each subclass of HTTPException on initialization of the F... — committed to untitaker/flask by untitaker 10 years ago
- Added support for generic HTTPException handlers on app and blueprints Error handlers are now returned in order of blueprint:code, app:code, blueprint:HTTPException, app:HTTPException, None Correspo... — committed to cerickson/flask by cerickson 7 years ago
- Added support for generic HTTPException handlers on app and blueprints Error handlers are now returned in order of blueprint:code, app:code, blueprint:HTTPException, app:HTTPException, None Correspo... — committed to cerickson/flask by cerickson 7 years ago
- Added support for generic HTTPException handlers on app and blueprints Error handlers are now returned in order of blueprint:code, app:code, blueprint:HTTPException, app:HTTPException, None Correspo... — committed to cerickson/flask by cerickson 7 years ago
For those who simply want to override the default behavior of the default
HTTPExceptionsubclasses, you can do something like this: