fixing static request handler logging exception on 404 (#2099)

* fixing static request handler logging exception when not necessary, adding test to verify exception is gone on 404

* Fixup tests

* Fix tests

* resolve test failure

Co-authored-by: Adam Hopkins <admhpkns@gmail.com>
This commit is contained in:
Frederik Gelder 2021-04-06 21:20:25 +02:00 committed by GitHub
parent b716f48c84
commit 8f06d035cb
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 46 additions and 10 deletions

View File

@ -718,16 +718,18 @@ class RouteMixin:
return await file(file_path, headers=headers, _range=_range) return await file(file_path, headers=headers, _range=_range)
except ContentRangeError: except ContentRangeError:
raise raise
except Exception: except FileNotFoundError:
error_logger.exception(
f"File not found: path={file_or_directory}, "
f"relative_url={__file_uri__}"
)
raise FileNotFound( raise FileNotFound(
"File not found", "File not found",
path=file_or_directory, path=file_or_directory,
relative_url=__file_uri__, relative_url=__file_uri__,
) )
except Exception:
error_logger.exception(
f"Exception in static request handler:\
path={file_or_directory}, "
f"relative_url={__file_uri__}"
)
def _register_static( def _register_static(
self, self,

View File

@ -6,13 +6,13 @@ from bs4 import BeautifulSoup
from sanic import Sanic from sanic import Sanic
from sanic.exceptions import ( from sanic.exceptions import (
SanicException,
Forbidden, Forbidden,
InvalidUsage, InvalidUsage,
NotFound, NotFound,
SanicException,
ServerError, ServerError,
Unauthorized, Unauthorized,
abort abort,
) )
from sanic.response import text from sanic.response import text
@ -231,4 +231,4 @@ def test_sanic_exception(exception_app):
with warnings.catch_warnings(record=True) as w: with warnings.catch_warnings(record=True) as w:
request, response = exception_app.test_client.get("/old_abort") request, response = exception_app.test_client.get("/old_abort")
assert response.status == 500 assert response.status == 500
assert len(w) == 1 and 'deprecated' in w[0].message.args[0] assert len(w) == 1 and "deprecated" in w[0].message.args[0]

View File

@ -1,10 +1,8 @@
import asyncio import asyncio
import httpcore import httpcore
import httpx import httpx
from sanic_testing.testing import SanicTestClient from sanic_testing.testing import SanicTestClient
from sanic import Sanic from sanic import Sanic

View File

@ -1,11 +1,16 @@
import inspect import inspect
import logging
import os import os
from collections import Counter
from pathlib import Path from pathlib import Path
from time import gmtime, strftime from time import gmtime, strftime
import pytest import pytest
from sanic import text
from sanic.exceptions import FileNotFound
@pytest.fixture(scope="module") @pytest.fixture(scope="module")
def static_file_directory(): def static_file_directory():
@ -454,3 +459,34 @@ def test_nested_dir(app, static_file_directory):
assert response.status == 200 assert response.status == 200
assert response.text == "foo\n" assert response.text == "foo\n"
def test_stack_trace_on_not_found(app, static_file_directory, caplog):
app.static("/static", static_file_directory)
with caplog.at_level(logging.INFO):
_, response = app.test_client.get("/static/non_existing_file.file")
counter = Counter([r[1] for r in caplog.record_tuples])
assert response.status == 404
assert counter[logging.INFO] == 5
assert counter[logging.ERROR] == 1
def test_no_stack_trace_on_not_found(app, static_file_directory, caplog):
app.static("/static", static_file_directory)
@app.exception(FileNotFound)
async def file_not_found(request, exception):
return text(f"No file: {request.path}", status=404)
with caplog.at_level(logging.INFO):
_, response = app.test_client.get("/static/non_existing_file.file")
counter = Counter([r[1] for r in caplog.record_tuples])
assert response.status == 404
assert counter[logging.INFO] == 5
assert logging.ERROR not in counter
assert response.text == "No file: /static/non_existing_file.file"