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

Blueprints not handling errors on 21.9.0 #2252

Closed
aericson opened this issue Oct 1, 2021 · 2 comments
Closed

Blueprints not handling errors on 21.9.0 #2252

aericson opened this issue Oct 1, 2021 · 2 comments

Comments

@aericson
Copy link
Contributor

aericson commented Oct 1, 2021

Describe the bug
Error handler on the blueprint is no longer working with 21.9.0. Worked fine on 21.6.2

Code snippet
I've created a reproducible simplified version on the repo here: https://github.com/aericson/sanic-blueprint-error-demo
You should be able to see the error on the Github Action here: https://github.com/aericson/sanic-blueprint-error-demo/actions/runs/1295855511

server:

##### blueprint
bp = Blueprint(__name__)


@bp.exception(Exception)
async def oops(request, exception):
    return text("OOops")
####### server
 def init_app():
    app = Sanic("MyHelloWorldApp")

    @app.get("/")
    async def hello_world(request):
        raise Exception("this is an exception")

    app.blueprint(bp)

    return app

Expected behavior
Expected a request to / to respond with the return of the error handler. Returned 500 instead.

Environment (please complete the following information):

  • OS: Any
  • Version 21.9.0
@prryplatypus
Copy link
Member

prryplatypus commented Oct 1, 2021

Your example is not supposed to work the way you intend it to. Blueprint exception handlers are only supposed to handle exceptions that happen within their own scope. In your example, you're attaching an error handler to the blueprint, but your route is being attached to the app instance directly, not the blueprint.

The fact that it used to work was an unintended side-effect/bug which was fixed here: #2246

@aericson
Copy link
Contributor Author

aericson commented Oct 1, 2021

Your example is not supposed to work the way you intend it to. Blueprint exception handlers are only supposed to handle exceptions that happen within their own scope. In your example, you're attaching an error handler to the blueprint, but your route is being attached to the app instance directly, not the blueprint.

The fact that it used to work was an unintended side-effect/bug which was fixed here: #2246

I see, thanks for the quick reply @prryplatypus!

We are currently just using blueprints for error handling. I guess our options would then be to either drop blueprints and attach handlers to the app or move the routing to the blueprints entirely.

A bit unfortunately but at least it's clear where our problem is at now. Thank you!

@aericson aericson closed this as completed Oct 1, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants