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:
		| @@ -718,16 +718,18 @@ class RouteMixin: | ||||
|                 return await file(file_path, headers=headers, _range=_range) | ||||
|         except ContentRangeError: | ||||
|             raise | ||||
|         except Exception: | ||||
|             error_logger.exception( | ||||
|                 f"File not found: path={file_or_directory}, " | ||||
|                 f"relative_url={__file_uri__}" | ||||
|             ) | ||||
|         except FileNotFoundError: | ||||
|             raise FileNotFound( | ||||
|                 "File not found", | ||||
|                 path=file_or_directory, | ||||
|                 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( | ||||
|         self, | ||||
|   | ||||
| @@ -6,13 +6,13 @@ from bs4 import BeautifulSoup | ||||
|  | ||||
| from sanic import Sanic | ||||
| from sanic.exceptions import ( | ||||
|     SanicException, | ||||
|     Forbidden, | ||||
|     InvalidUsage, | ||||
|     NotFound, | ||||
|     SanicException, | ||||
|     ServerError, | ||||
|     Unauthorized, | ||||
|     abort | ||||
|     abort, | ||||
| ) | ||||
| from sanic.response import text | ||||
|  | ||||
| @@ -231,4 +231,4 @@ def test_sanic_exception(exception_app): | ||||
|     with warnings.catch_warnings(record=True) as w: | ||||
|         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] | ||||
|     assert len(w) == 1 and "deprecated" in w[0].message.args[0] | ||||
|   | ||||
| @@ -1,10 +1,8 @@ | ||||
| import asyncio | ||||
|  | ||||
|  | ||||
| import httpcore | ||||
| import httpx | ||||
|  | ||||
|  | ||||
| from sanic_testing.testing import SanicTestClient | ||||
|  | ||||
| from sanic import Sanic | ||||
|   | ||||
| @@ -1,11 +1,16 @@ | ||||
| import inspect | ||||
| import logging | ||||
| import os | ||||
|  | ||||
| from collections import Counter | ||||
| from pathlib import Path | ||||
| from time import gmtime, strftime | ||||
|  | ||||
| import pytest | ||||
|  | ||||
| from sanic import text | ||||
| from sanic.exceptions import FileNotFound | ||||
|  | ||||
|  | ||||
| @pytest.fixture(scope="module") | ||||
| def static_file_directory(): | ||||
| @@ -454,3 +459,34 @@ def test_nested_dir(app, static_file_directory): | ||||
|  | ||||
|     assert response.status == 200 | ||||
|     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" | ||||
|   | ||||
		Reference in New Issue
	
	Block a user
	 Frederik Gelder
					Frederik Gelder