Better request cancel handling (#2513)

This commit is contained in:
Adam Hopkins
2022-09-19 16:04:09 +03:00
committed by GitHub
parent 7f894c45b3
commit 389363ab71
7 changed files with 46 additions and 8 deletions

View File

@@ -0,0 +1,21 @@
import asyncio
from asyncio import CancelledError
import pytest
from sanic import Request, Sanic, json
def test_can_raise_in_handler(app: Sanic):
@app.get("/")
async def handler(request: Request):
raise CancelledError("STOP!!")
@app.exception(CancelledError)
async def handle_cancel(request: Request, exc: CancelledError):
return json({"message": exc.args[0]}, status=418)
_, response = app.test_client.get("/")
assert response.status == 418
assert response.json["message"] == "STOP!!"

View File

@@ -166,7 +166,7 @@ def test_middleware_response_raise_cancelled_error(app, caplog):
with caplog.at_level(logging.ERROR):
reqrequest, response = app.test_client.get("/")
assert response.status == 503
assert response.status == 500
assert (
"sanic.error",
logging.ERROR,