v0.8测试

This commit is contained in:
2026-04-16 07:55:46 +08:00
parent 7c743293be
commit c8262cf92e
16 changed files with 84 additions and 39 deletions

View File

@@ -85,13 +85,28 @@ app.add_middleware(
async def global_exception_handler(request: Request, exc: Exception):
"""全局异常处理器 - 捕获所有未处理异常"""
logger.error(f"未处理异常: {exc}", exc_info=True)
# 获取origin用于CORS头
origin = request.headers.get("origin", "")
allowed_origins = settings.CORS_ORIGINS or []
# 使用HTTP 200 + 业务错误码返回避免CORS头丢失问题
# FastAPI exception_handler返回的500响应可能不经过CORS中间件导致跨域读取失败
headers = {}
if origin in allowed_origins:
headers["access-control-allow-origin"] = origin
headers["access-control-allow-credentials"] = "true"
headers["access-control-expose-headers"] = "*"
return JSONResponse(
status_code=500,
status_code=200,
content={
"success": False,
"code": 500,
"message": f"服务器内部错误: {str(exc)}",
"detail": traceback.format_exc() if settings.DEBUG else None
}
},
headers=headers
)