Account for BP with exception handler but no routes (#2246)
This commit is contained in:
parent
6ffc4d9756
commit
ba2670e99c
@ -331,21 +331,22 @@ class Blueprint(BaseSanic):
|
|||||||
|
|
||||||
route_names = [route.name for route in routes if route]
|
route_names = [route.name for route in routes if route]
|
||||||
|
|
||||||
# Middleware
|
|
||||||
if route_names:
|
if route_names:
|
||||||
|
# Middleware
|
||||||
for future in self._future_middleware:
|
for future in self._future_middleware:
|
||||||
middleware.append(app._apply_middleware(future, route_names))
|
middleware.append(app._apply_middleware(future, route_names))
|
||||||
|
|
||||||
# Exceptions
|
# Exceptions
|
||||||
for future in self._future_exceptions:
|
for future in self._future_exceptions:
|
||||||
exception_handlers.append(
|
exception_handlers.append(
|
||||||
app._apply_exception_handler(future, route_names)
|
app._apply_exception_handler(future, route_names)
|
||||||
)
|
)
|
||||||
|
|
||||||
# Event listeners
|
# Event listeners
|
||||||
for listener in self._future_listeners:
|
for listener in self._future_listeners:
|
||||||
listeners[listener.event].append(app._apply_listener(listener))
|
listeners[listener.event].append(app._apply_listener(listener))
|
||||||
|
|
||||||
|
# Signals
|
||||||
for signal in self._future_signals:
|
for signal in self._future_signals:
|
||||||
signal.condition.update({"blueprint": self.name})
|
signal.condition.update({"blueprint": self.name})
|
||||||
app._apply_signal(signal)
|
app._apply_signal(signal)
|
||||||
|
@ -83,7 +83,6 @@ def test_versioned_routes_get(app, method):
|
|||||||
return text("OK")
|
return text("OK")
|
||||||
|
|
||||||
else:
|
else:
|
||||||
print(func)
|
|
||||||
raise Exception(f"{func} is not callable")
|
raise Exception(f"{func} is not callable")
|
||||||
|
|
||||||
app.blueprint(bp)
|
app.blueprint(bp)
|
||||||
@ -477,6 +476,58 @@ def test_bp_exception_handler(app):
|
|||||||
assert response.status == 200
|
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):
|
def test_bp_listeners(app):
|
||||||
app.route("/")(lambda x: x)
|
app.route("/")(lambda x: x)
|
||||||
blueprint = Blueprint("test_middleware")
|
blueprint = Blueprint("test_middleware")
|
||||||
|
Loading…
x
Reference in New Issue
Block a user