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
{{ message }}
Repository navigation
Allow blueprint-local routing for errorhandlers #503
The comment in flask.Blueprint.errorhandler notes, "Please be aware that routing does not happen local to a blueprint so an error handler for 404 usually is not handled by a blueprint unless it is caused inside a view function."
Please add Blueprint-local routing for errors triggered by any request to an endpoint defined by a blueprint.
This issue supercedes #141, which requests Module-local routing for errorhandlers (the Module class was deprecated in favor of Blueprint in Flask version 0.7).
+1 this would be amazing. To add more context, I develop flask-restful, a module for building REST API in Flask. We'd like to move to using blueprints, but this limit makes it very difficult for us to handle errors.
Yep, it's a difficult problem to solve. I've been thinking about using a bare expect (ugly I know) to catch all errors in my views and wrap them in a custom Exception that my blueprint handler can catch. Not sure if that's a good idea or not.
This came up in a recent Stack Overflow question. I mentioned this issue and answered with a workaround that routes to blueprint handlers from the top level handler based on request.path and Blueprint.url_prefix.
fromflaskimportrequest, render_template@app.errorhandler(404)defhandle_404(e):
path=request.path# go through each blueprint to find the prefix that matches the path# can't use request.blueprint since the routing didn't match anythingforbp_name, bpinapp.blueprints.items():
ifpath.startswith(bp.url_prefix):
# get the 404 handler registered by the blueprinthandler=app.error_handler_spec.get(bp_name, {}).get(404)
ifhandlerisnotNone:
# if a handler was found, return it's responsereturnhandler(e)
# return a default responsereturnrender_template('404.html'), 404
It seems like the test function test_error_handler_blueprint demonstrates blueprint-local routing for error handlers as originally requested. Feel free to close this issue.
It is simply impossible to have blueprint-level 404 error handlers as a blueprint does not "claim" a certain URL prefix (even when specifying one - that's simply the prefix for any of the URLs it handles, but it does not need to be unique among blueprints).
Looking up blueprint error handlers based on their URL prefix would have to be optional for sure.
Activity
cevarief commentedon Jun 27, 2012
+1
jfinkels commentedon Jun 27, 2012
This issue supercedes #141, which requests
Module-local routing for errorhandlers (theModuleclass was deprecated in favor ofBlueprintin Flask version 0.7).skymanrm commentedon Sep 25, 2012
+1
kyleconroy commentedon Nov 19, 2012
+1 this would be amazing. To add more context, I develop flask-restful, a module for building REST API in Flask. We'd like to move to using blueprints, but this limit makes it very difficult for us to handle errors.
jfinkels commentedon Nov 29, 2012
@kyleconroy I came across this issue because I maintain a very similar Flask extension, Flask-Restless, and I have the same problem.
kyleconroy commentedon Dec 3, 2012
Yep, it's a difficult problem to solve. I've been thinking about using a bare expect (ugly I know) to catch all errors in my views and wrap them in a custom Exception that my blueprint handler can catch. Not sure if that's a good idea or not.
esbullington commentedon Dec 29, 2012
This would be great. I think more and more people are turning to Flask for developing json-based APIs (RESTful and otherwise). It would be nice if clients of these APIs were not sometimes sent HTML when they trigger an error not explicitly handled by the API code. It makes for a poor API. There is a workaround here: http://flask.pocoo.org/snippets/83/ Alas, this isn't an option for people creating hybrid apps that both HTML and provide a JSON API, as far as I can tell.
davidism commentedon Jan 25, 2015
This came up in a recent Stack Overflow question. I mentioned this issue and answered with a workaround that routes to blueprint handlers from the top level handler based on
request.pathandBlueprint.url_prefix.untitaker commentedon Jan 25, 2015
If anybody is interested in doing this, now would be the ideal time, since the next release is going to be 1.0.
untitaker commentedon Jan 25, 2015
Everybody who has any problems with the way errorhandlers work now, please leave your comments at #1291
flying-sheep commentedon Apr 11, 2015
i did #1291 quite some time ago so idk if i “accidentally” fixed this in the run.
what can i do to check? please give me a simple test that i can run to see if this bug occurs. (actual running code pls)
flying-sheep commentedon Apr 11, 2015
so, in that pull request i said “Enable 500 handlers on blueprint level” so probably it fixes this already.
jfinkels commentedon Apr 12, 2015
It seems like the test function
test_error_handler_blueprintdemonstrates blueprint-local routing for error handlers as originally requested. Feel free to close this issue.jfinkels commentedon Apr 12, 2015
(I just tested that it works with errors other than 500 as well.)
untitaker commentedon Apr 12, 2015
Yep.
tdbs commentedon Feb 3, 2017
This still doesn't work for 404 errors
ThiefMaster commentedon Feb 3, 2017
It is simply impossible to have blueprint-level 404 error handlers as a blueprint does not "claim" a certain URL prefix (even when specifying one - that's simply the prefix for any of the URLs it handles, but it does not need to be unique among blueprints).
Looking up blueprint error handlers based on their URL prefix would have to be optional for sure.