Fix bug where ws exceptions not being logged (#2213)

* Fix bug where ws exceptions not being logged

* Fix t\est
This commit is contained in:
Adam Hopkins
2021-08-07 23:24:48 +03:00
committed by GitHub
parent 71a631237d
commit 8dbda247d6
4 changed files with 27 additions and 12 deletions

View File

@@ -360,6 +360,7 @@ async def test_request_handle_exception(app):
_, response = await app.asgi_client.get("/error-prone")
assert response.status_code == 503
@pytest.mark.asyncio
async def test_request_exception_suppressed_by_middleware(app):
@app.get("/error-prone")
@@ -374,4 +375,4 @@ async def test_request_exception_suppressed_by_middleware(app):
assert response.status_code == 403
_, response = await app.asgi_client.get("/error-prone")
assert response.status_code == 403
assert response.status_code == 403

View File

@@ -1,3 +1,4 @@
import logging
import warnings
import pytest
@@ -232,3 +233,20 @@ def test_sanic_exception(exception_app):
request, response = exception_app.test_client.get("/old_abort")
assert response.status == 500
assert len(w) == 1 and "deprecated" in w[0].message.args[0]
def test_exception_in_ws_logged(caplog):
app = Sanic(__file__)
@app.websocket("/feed")
async def feed(request, ws):
raise Exception("...")
with caplog.at_level(logging.INFO):
app.test_client.websocket("/feed")
assert caplog.record_tuples[1][0] == "sanic.error"
assert caplog.record_tuples[1][1] == logging.ERROR
assert (
"Exception occurred while handling uri:" in caplog.record_tuples[1][2]
)