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

Account for BP with exception handler but no routes #2246

Merged
merged 3 commits into from
Sep 29, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 5 additions & 4 deletions sanic/blueprints.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
ahopkins marked this conversation as resolved.
Show resolved Hide resolved
for future in self._future_exceptions:
exception_handlers.append(
app._apply_exception_handler(future, route_names)
)

# Event listeners
for listener in self._future_listeners:
Expand Down
53 changes: 52 additions & 1 deletion tests/test_blueprints.py
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,6 @@ def handler(request):
return text("OK")

else:
print(func)
raise Exception(f"{func} is not callable")

app.blueprint(bp)
Expand Down Expand Up @@ -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")
Expand Down