From f8a9197474303ccb14fa7695d52ca9d356dbc1fc Mon Sep 17 00:00:00 2001 From: Leo Vasanko Date: Thu, 16 Nov 2023 17:10:18 -0800 Subject: [PATCH] Tidying up log, correct HTTP status for error pages. --- cista/app.py | 2 -- cista/util/apphelpers.py | 7 +++++-- 2 files changed, 5 insertions(+), 4 deletions(-) diff --git a/cista/app.py b/cista/app.py index 4d4e1bb..c36a858 100644 --- a/cista/app.py +++ b/cista/app.py @@ -1,6 +1,5 @@ import asyncio import datetime -import logging import mimetypes import threading from concurrent.futures import ThreadPoolExecutor @@ -33,7 +32,6 @@ app.exception(Exception)(handle_sanic_exception) @app.before_server_start async def main_start(app, loop): config.load_config() - logger.setLevel(logging.INFO) app.ctx.threadexec = ThreadPoolExecutor( max_workers=8, thread_name_prefix="cista-ioworker" ) diff --git a/cista/util/apphelpers.py b/cista/util/apphelpers.py index c0433b1..786c7f1 100644 --- a/cista/util/apphelpers.py +++ b/cista/util/apphelpers.py @@ -41,7 +41,9 @@ async def handle_sanic_exception(request, e): res.cookies.add_cookie("message", message, max_age=5) return res # Otherwise use Sanic's default error page - return errorpages.HTMLRenderer(request, e, debug=request.app.debug).full() + res = errorpages.HTMLRenderer(request, e, debug=request.app.debug).full() + res.status = status # Unfortunately Sanic <23.12 doesn't set this + return res def websocket_wrapper(handler): @@ -53,13 +55,14 @@ def websocket_wrapper(handler): auth.verify(request) await handler(request, ws, *args, **kwargs) except Exception as e: - logger.exception(e) context, code, message = {}, 500, str(e) or "Internal Server Error" if isinstance(e, SanicException): context = e.context or {} code = e.status_code message = f"⚠️ {message}" if code < 500 else f"🛑 {message}" await asend(ws, ErrorMsg({"code": code, "message": message, **context})) + if not getattr(e, "quiet", False): + logger.exception(f"{code} {e!r}") raise return wrapper