-
-
Notifications
You must be signed in to change notification settings - Fork 16.2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Allow blueprint-local routing for errorhandlers #503
Comments
+1 |
This issue supercedes #141, which requests |
+1 |
+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. |
@kyleconroy I came across this issue because I maintain a very similar Flask extension, Flask-Restless, and I have the same problem. |
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 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. |
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 from flask import request, render_template
@app.errorhandler(404)
def handle_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 anything
for bp_name, bp in app.blueprints.items():
if path.startswith(bp.url_prefix):
# get the 404 handler registered by the blueprint
handler = app.error_handler_spec.get(bp_name, {}).get(404)
if handler is not None:
# if a handler was found, return it's response
return handler(e)
# return a default response
return render_template('404.html'), 404 |
If anybody is interested in doing this, now would be the ideal time, since the next release is going to be 1.0. |
Everybody who has any problems with the way errorhandlers work now, please leave your comments at #1291 |
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) |
so, in that pull request i said “Enable 500 handlers on blueprint level” so probably it fixes this already. |
It seems like the test function |
(I just tested that it works with errors other than 500 as well.) |
This still doesn't work for 404 errors |
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. |
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.
The text was updated successfully, but these errors were encountered: