Skip to content
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

Closed
jfinkels opened this issue May 4, 2012 · 17 comments
Closed

Allow blueprint-local routing for errorhandlers #503

jfinkels opened this issue May 4, 2012 · 17 comments

Comments

@jfinkels
Copy link
Contributor

jfinkels commented May 4, 2012

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.

@cevarief
Copy link

+1

@jfinkels
Copy link
Contributor Author

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).

@skymanrm
Copy link

+1

@kyleconroy
Copy link

+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
Copy link
Contributor Author

@kyleconroy I came across this issue because I maintain a very similar Flask extension, Flask-Restless, and I have the same problem.

@kyleconroy
Copy link

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
Copy link

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
Copy link
Member

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.

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

@untitaker
Copy link
Contributor

If anybody is interested in doing this, now would be the ideal time, since the next release is going to be 1.0.

@untitaker
Copy link
Contributor

Everybody who has any problems with the way errorhandlers work now, please leave your comments at #1291

@flying-sheep
Copy link
Contributor

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
Copy link
Contributor

so, in that pull request i said “Enable 500 handlers on blueprint level” so probably it fixes this already.

@jfinkels
Copy link
Contributor Author

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.

@jfinkels
Copy link
Contributor Author

(I just tested that it works with errors other than 500 as well.)

@untitaker
Copy link
Contributor

Yep.

@tdbs
Copy link

tdbs commented Feb 3, 2017

This still doesn't work for 404 errors

@ThiefMaster
Copy link
Member

ThiefMaster commented 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.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Nov 14, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

10 participants