Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

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

Comments

Projects
None yet
10 participants
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.

+1

Contributor

jfinkels commented Jun 27, 2012

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

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

Contributor

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

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.

Owner

davidism commented 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.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
Owner

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

Owner

untitaker commented Jan 25, 2015

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

Contributor

flying-sheep commented 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)

Contributor

flying-sheep commented Apr 11, 2015

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

Contributor

jfinkels commented Apr 12, 2015

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.

Contributor

jfinkels commented Apr 12, 2015

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

Owner

untitaker commented Apr 12, 2015

@untitaker untitaker closed this Apr 12, 2015

tdbs commented Feb 3, 2017

This still doesn't work for 404 errors

Owner

ThiefMaster commented Feb 3, 2017 edited

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.

@drjova drjova referenced this issue in CERNDocumentServer/cds-videos Feb 20, 2017

Closed

error pages #452

1 of 2 tasks complete
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment