Account for BP with exception handler but no routes (#2246)

This commit is contained in:
Adam Hopkins 2021-09-29 13:47:31 +03:00 committed by GitHub
parent 6ffc4d9756
commit ba2670e99c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 59 additions and 7 deletions

View File

@ -331,21 +331,22 @@ class Blueprint(BaseSanic):
route_names = [route.name for route in routes if route]
# Middleware
if route_names:
# Middleware
for future in self._future_middleware:
middleware.append(app._apply_middleware(future, route_names))
# Exceptions
for future in self._future_exceptions:
exception_handlers.append(
app._apply_exception_handler(future, route_names)
)
# Exceptions
for future in self._future_exceptions:
exception_handlers.append(
app._apply_exception_handler(future, route_names)
)
# Event listeners
for listener in self._future_listeners:
listeners[listener.event].append(app._apply_listener(listener))
# Signals
for signal in self._future_signals:
signal.condition.update({"blueprint": self.name})
app._apply_signal(signal)

View File

@ -83,7 +83,6 @@ def test_versioned_routes_get(app, method):
return text("OK")
else:
print(func)
raise Exception(f"{func} is not callable")
app.blueprint(bp)
@ -477,6 +476,58 @@ def test_bp_exception_handler(app):
assert response.status == 200
def test_bp_exception_handler_applied(app):
class Error(Exception):
pass
handled = Blueprint("handled")
nothandled = Blueprint("nothandled")
@handled.exception(Error)
def handle_error(req, e):
return text("handled {}".format(e))
@handled.route("/ok")
def ok(request):
raise Error("uh oh")
@nothandled.route("/notok")
def notok(request):
raise Error("uh oh")
app.blueprint(handled)
app.blueprint(nothandled)
_, response = app.test_client.get("/ok")
assert response.status == 200
assert response.text == "handled uh oh"
_, response = app.test_client.get("/notok")
assert response.status == 500
def test_bp_exception_handler_not_applied(app):
class Error(Exception):
pass
handled = Blueprint("handled")
nothandled = Blueprint("nothandled")
@handled.exception(Error)
def handle_error(req, e):
return text("handled {}".format(e))
@nothandled.route("/notok")
def notok(request):
raise Error("uh oh")
app.blueprint(handled)
app.blueprint(nothandled)
_, response = app.test_client.get("/notok")
assert response.status == 500
def test_bp_listeners(app):
app.route("/")(lambda x: x)
blueprint = Blueprint("test_middleware")