From 3cc35c03363b3eadd6ce18c1363fc7c3b10878ee Mon Sep 17 00:00:00 2001 From: chengkai3 Date: Thu, 18 Jun 2026 08:39:21 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20[FL-184]=20=E6=B7=BB=E5=8A=A0=E8=B0=83?= =?UTF-8?q?=E8=AF=95=E6=A8=A1=E5=BC=8F=E9=85=8D=E7=BD=AE=E4=BB=A5=E8=BF=94?= =?UTF-8?q?=E5=9B=9E=E5=BC=82=E5=B8=B8=E5=A0=86=E6=A0=88=E8=B7=9F=E8=B8=AA?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 在配置文件中添加 debug_mode 参数,默认值为 true - 创建全局异常处理器,当 debug_mode 开启时返回 stacktrace 信息 - 在 .env.example 中添加 DEBUG_MODE 配置项说明 - 新增测试文件验证调试模式功能 Co-Authored-By: Claude Sonnet 4.6 Co-authored-by: multica-agent --- .env.example | 1 + api/app/core/config.py | 2 + api/app/core/exception_handlers.py | 22 +++++++++++ api/app/main.py | 3 ++ api/tests/test_debug_mode.py | 59 ++++++++++++++++++++++++++++++ api/verify_debug_mode.py | 45 +++++++++++++++++++++++ 6 files changed, 132 insertions(+) create mode 100644 api/app/core/exception_handlers.py create mode 100644 api/tests/test_debug_mode.py create mode 100644 api/verify_debug_mode.py diff --git a/.env.example b/.env.example index f424f05..0886803 100644 --- a/.env.example +++ b/.env.example @@ -49,6 +49,7 @@ WINE_MAX_TIMEOUT_SECONDS=1800 INITIAL_ADMIN_EMAIL=admin@example.com INITIAL_ADMIN_USERNAME=admin INITIAL_ADMIN_PASSWORD=change-me-strong-password +DEBUG_MODE=true POSTGRES_DB=fquiz POSTGRES_USER=fquiz POSTGRES_PASSWORD=fquiz diff --git a/api/app/core/config.py b/api/app/core/config.py index db3a7ce..e36e215 100644 --- a/api/app/core/config.py +++ b/api/app/core/config.py @@ -74,6 +74,8 @@ class Settings(BaseSettings): initial_admin_username: str = "管理员" initial_admin_password: str | None = None + debug_mode: bool = True + model_config = SettingsConfigDict( env_file=".env", env_file_encoding="utf-8", diff --git a/api/app/core/exception_handlers.py b/api/app/core/exception_handlers.py new file mode 100644 index 0000000..f069780 --- /dev/null +++ b/api/app/core/exception_handlers.py @@ -0,0 +1,22 @@ +import traceback +from fastapi import Request, status +from fastapi.responses import JSONResponse + +from .config import get_settings + + +async def global_exception_handler(request: Request, exc: Exception) -> JSONResponse: + settings = get_settings() + + error_response = { + "detail": str(exc), + "type": type(exc).__name__, + } + + if settings.debug_mode: + error_response["stacktrace"] = traceback.format_exc() + + return JSONResponse( + status_code=status.HTTP_500_INTERNAL_SERVER_ERROR, + content=error_response, + ) diff --git a/api/app/main.py b/api/app/main.py index eff68bd..26dfe38 100644 --- a/api/app/main.py +++ b/api/app/main.py @@ -6,6 +6,7 @@ from fastapi.middleware.cors import CORSMiddleware from .api.router import api_router from .core.config import get_settings from .core.database import SessionLocal, init_db +from .core.exception_handlers import global_exception_handler from .services.scheduled_task_service import seed_default_scheduled_tasks settings = get_settings() @@ -47,4 +48,6 @@ def health() -> dict[str, str]: "version": settings.api_version, } +app.add_exception_handler(Exception, global_exception_handler) + app.include_router(api_router) diff --git a/api/tests/test_debug_mode.py b/api/tests/test_debug_mode.py new file mode 100644 index 0000000..a53de3e --- /dev/null +++ b/api/tests/test_debug_mode.py @@ -0,0 +1,59 @@ +"""Test debug mode configuration and exception handling.""" +from unittest.mock import patch + +from fastapi import Request +from fastapi.responses import JSONResponse + +from app.core.config import Settings, get_settings +from app.core.exception_handlers import global_exception_handler + + +def test_debug_mode_default_enabled(): + """Test that debug_mode defaults to True.""" + settings = Settings() + assert settings.debug_mode is True + + +def test_debug_mode_can_be_disabled(): + """Test that debug_mode can be disabled via environment.""" + with patch.dict("os.environ", {"DEBUG_MODE": "false"}): + settings = Settings() + assert settings.debug_mode is False + + +async def test_exception_handler_with_debug_enabled(): + """Test exception handler includes stacktrace when debug is enabled.""" + with patch("app.core.exception_handlers.get_settings") as mock_settings: + mock_settings.return_value.debug_mode = True + + request = None + exc = ValueError("Test error") + + response = await global_exception_handler(request, exc) + + assert isinstance(response, JSONResponse) + assert response.status_code == 500 + + content = response.body.decode() + assert "Test error" in content + assert "ValueError" in content + assert "stacktrace" in content + + +async def test_exception_handler_with_debug_disabled(): + """Test exception handler excludes stacktrace when debug is disabled.""" + with patch("app.core.exception_handlers.get_settings") as mock_settings: + mock_settings.return_value.debug_mode = False + + request = None + exc = ValueError("Test error") + + response = await global_exception_handler(request, exc) + + assert isinstance(response, JSONResponse) + assert response.status_code == 500 + + content = response.body.decode() + assert "Test error" in content + assert "ValueError" in content + assert "stacktrace" not in content diff --git a/api/verify_debug_mode.py b/api/verify_debug_mode.py new file mode 100644 index 0000000..71fd962 --- /dev/null +++ b/api/verify_debug_mode.py @@ -0,0 +1,45 @@ +#!/usr/bin/env python3 +"""Verify debug mode configuration and exception handler.""" +import sys +import os +sys.path.insert(0, os.path.join(os.path.dirname(__file__), "app")) + +from app.core.config import get_settings + +def main(): + settings = get_settings() + + print("Configuration Verification:") + print(f" debug_mode: {settings.debug_mode}") + print(f" api_name: {settings.api_name}") + print(f" api_version: {settings.api_version}") + + # Test exception handler import + try: + from app.core.exception_handlers import global_exception_handler + print("\n✓ Exception handler imported successfully") + except ImportError as e: + print(f"\n✗ Failed to import exception handler: {e}") + return 1 + + # Test main app import + try: + from app.main import app + print("✓ Main app imported successfully") + + # Check if exception handler is registered + exception_handlers = app.exception_handlers + if Exception in exception_handlers: + print("✓ Global exception handler is registered") + else: + print("✗ Global exception handler is NOT registered") + return 1 + except ImportError as e: + print(f"✗ Failed to import main app: {e}") + return 1 + + print("\n✓ All checks passed!") + return 0 + +if __name__ == "__main__": + sys.exit(main())