From 9c753d1671f6929a079b1088a2d1a3c071db0597 Mon Sep 17 00:00:00 2001 From: Adam Hopkins Date: Mon, 27 Sep 2021 12:54:35 +0300 Subject: [PATCH 1/2] Account for BP with exception handler but no routes --- sanic/blueprints.py | 9 ++++--- tests/test_blueprints.py | 53 +++++++++++++++++++++++++++++++++++++++- 2 files changed, 57 insertions(+), 5 deletions(-) diff --git a/sanic/blueprints.py b/sanic/blueprints.py index 31809acf74..57426a865a 100644 --- a/sanic/blueprints.py +++ b/sanic/blueprints.py @@ -337,10 +337,11 @@ def register(self, app, options): 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) - ) + if route_names: + for future in self._future_exceptions: + exception_handlers.append( + app._apply_exception_handler(future, route_names) + ) # Event listeners for listener in self._future_listeners: diff --git a/tests/test_blueprints.py b/tests/test_blueprints.py index fec7b50a41..0ffff197cf 100644 --- a/tests/test_blueprints.py +++ b/tests/test_blueprints.py @@ -83,7 +83,6 @@ def handler(request): return text("OK") else: - print(func) raise Exception(f"{func} is not callable") app.blueprint(bp) @@ -477,6 +476,58 @@ def handler_exception(request, exception): 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") From 8adfd2624548ad321aacd29c9348f8d217316930 Mon Sep 17 00:00:00 2001 From: Adam Hopkins Date: Tue, 28 Sep 2021 20:10:41 +0300 Subject: [PATCH 2/2] Reindent loops --- sanic/blueprints.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/sanic/blueprints.py b/sanic/blueprints.py index 57426a865a..fd7332aaf0 100644 --- a/sanic/blueprints.py +++ b/sanic/blueprints.py @@ -331,13 +331,12 @@ def register(self, app, options): 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 - if route_names: + # Exceptions for future in self._future_exceptions: exception_handlers.append( app._apply_exception_handler(future, route_names) @@ -347,6 +346,7 @@ def register(self, app, options): 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)