Cannot define a blueprint-specific exception handler
See original GitHub issueDescribe the bug The documentation at https://sanicframework.org/en/guide/best-practices/blueprints.html#exceptions says that:
Just like other exception handling, you can define blueprint specific handlers.
However, exception handlers defined this way don’t seem to be blueprint-specific at all. Instead, they handle exceptions in other blueprints as well.
Code snippet
#!/usr/bin/env python3
from sanic import Sanic, Blueprint, response
class Error(Exception):
pass
handled = Blueprint("handled")
@handled.exception(Error)
def handle_error(req, e):
return response.text("handled {}".format(e))
b = Blueprint("b")
@b.route("/")
def e(request):
raise Error("error in e")
app = Sanic(__name__)
app.blueprint(handled)
app.blueprint(b)
app.run()
Expected behavior
A request to http://localhost:8000 should generate 500, because Error
should not be handled.
Instead, the request generates 200, Error
is handled, even though the handler is registered in handled
, while the endpoint is in b
.
Environment (please complete the following information):
- OS: ubuntu 19.10
- Version: Sanic 21.6.0; Routing 0.7.0
Additional context
The order of blueprint registration does not seem to change anything. Not registering blueprint handled
obviously fixes the issue.
Is it a bug or am I missing something here?
Issue Analytics
- State:
- Created 2 years ago
- Reactions:1
- Comments:5 (3 by maintainers)
Top GitHub Comments
See #2246 with a fix to this.
Thanks,
it works as expected.
The code from the OP is indeed not a sensible use case, just the simplest reproduction path.