Skip to content

Allow blueprint-local routing for errorhandlers #503

Closed

Description

@jfinkels

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.

Activity

  1. cevarief commented on Jun 27, 2012

    @cevarief

    +1

  2. jfinkels commented on Jun 27, 2012

    @jfinkels
    ContributorAuthor

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

  3. skymanrm commented on Sep 25, 2012

    @skymanrm

    +1

  4. kyleconroy commented on Nov 19, 2012

    @kyleconroy

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

  5. jfinkels commented on Nov 29, 2012

    @jfinkels
    ContributorAuthor

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

  6. kyleconroy commented on Dec 3, 2012

    @kyleconroy

    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.

  7. esbullington commented on Dec 29, 2012

    @esbullington

    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.

  8. davidism commented on Jan 25, 2015

    @davidism
    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
  9. untitaker commented on Jan 25, 2015

    @untitaker
    Contributor

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

  10. untitaker commented on Jan 25, 2015

    @untitaker
    Contributor

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

  11. flying-sheep commented on Apr 11, 2015

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

  12. flying-sheep commented on Apr 11, 2015

    @flying-sheep
    Contributor

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

  13. jfinkels commented on Apr 12, 2015

    @jfinkels
    ContributorAuthor

    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.

  14. jfinkels commented on Apr 12, 2015

    @jfinkels
    ContributorAuthor

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

  15. untitaker commented on Apr 12, 2015

    @untitaker
    Contributor
  16. tdbs commented on Feb 3, 2017

    @tdbs

    This still doesn't work for 404 errors

  17. ThiefMaster commented on Feb 3, 2017

    @ThiefMaster
    Member

    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.

  18. locked as resolved and limited conversation to collaborators on Nov 14, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions