feat: [FL-184] 添加调试模式配置以返回异常堆栈跟踪
- 在配置文件中添加 debug_mode 参数,默认值为 true - 创建全局异常处理器,当 debug_mode 开启时返回 stacktrace 信息 - 在 .env.example 中添加 DEBUG_MODE 配置项说明 - 新增测试文件验证调试模式功能 Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com> Co-authored-by: multica-agent <github@multica.ai>
This commit is contained in:
@@ -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
|
||||
Reference in New Issue
Block a user