Files
fquiz/api/app/core/exception_handlers.py
T
chengkai3 a332ba58ba [fix]:[FL-190][系统接口中的stacktrace信息不要直接展示到页面给用户看]
- 后端:在 exception_handlers.py 添加文档注释,明确说明 stacktrace 字段仅供开发调试使用
- 前端:在 api.ts 添加 ApiErrorResponse 类型定义和文档注释,明确 readApiError 函数只提取 detail 字段展示给用户
- stacktrace 在 debug 模式下会返回给浏览器(可在开发者工具中查看),但前端不会展示到页面上

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
Co-authored-by: multica-agent <github@multica.ai>
2026-06-18 10:19:25 +08:00

32 lines
953 B
Python

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:
"""
全局异常处理器
返回的错误响应包含:
- detail: 错误描述信息(面向用户,前端会展示此字段)
- type: 异常类型名称
- stacktrace: 调用栈信息(仅在debug模式下返回,仅供开发人员调试使用,前端不应展示给用户)
"""
settings = get_settings()
error_response = {
"detail": str(exc),
"type": type(exc).__name__,
}
# stacktrace 仅用于开发调试,不应直接展示到页面给用户看
if settings.debug_mode:
error_response["stacktrace"] = traceback.format_exc()
return JSONResponse(
status_code=status.HTTP_500_INTERNAL_SERVER_ERROR,
content=error_response,
)