dingdanquanliucheng/backend/app/main.py
taiyi 01f7a38094 fix: 订单明细迁移补全 + 全局异常处理 + Vue 模板修复
- 新增迁移 20260531_0001 补全 sales_order_item 报价快照列(幂等)
- 添加全局未捕获异常处理器,500 错误返回具体异常信息并记录 traceback
- 修复 web-sales LoginPage 和 CustomersPage 中 import 语句误写在 <template> 内的问题

Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
2026-05-31 14:07:28 +08:00

71 lines
2.1 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

"""FastAPI 应用入口。
创建应用实例、配置 CORS 中间件、注册全局异常处理器、挂载 API 路由。
启动命令: uvicorn backend.app.main:app --reload
"""
import logging
import traceback
from fastapi import FastAPI
from fastapi import Request
from fastapi.middleware.cors import CORSMiddleware
from fastapi.responses import JSONResponse
from backend.app.api.routes import api_router
from backend.app.core.config import get_settings
from backend.app.core.exceptions import AppException
logger = logging.getLogger(__name__)
settings = get_settings()
app = FastAPI(title=settings.app_name)
# 配置 CORS 跨域,允许前端开发服务器访问
app.add_middleware(
CORSMiddleware,
allow_origins=settings.cors_origins,
allow_credentials=True,
allow_methods=["*"],
allow_headers=["*"],
)
@app.exception_handler(AppException)
async def app_exception_handler(_: Request, exc: AppException) -> JSONResponse:
"""全局捕获 AppException返回统一的错误 JSON 格式。
避免每个路由重复构造错误响应,所有业务异常由此处理器统一格式化。
"""
# 统一业务异常返回格式,避免每个接口重复构造错误响应。
return JSONResponse(
status_code=exc.status_code,
content={
"code": exc.code,
"message": exc.message,
"data": {},
},
)
@app.exception_handler(Exception)
async def unhandled_exception_handler(_: Request, exc: Exception) -> JSONResponse:
"""捕获所有未处理异常,记录完整 traceback 到日志并返回 500。
避免 FastAPI 默认的 bare 500 响应,让运维能从日志中定位问题。
"""
logger.error("Unhandled exception: %s\n%s", exc, traceback.format_exc())
return JSONResponse(
status_code=500,
content={
"code": 500,
"message": f"服务器内部错误: {type(exc).__name__}: {exc}",
"data": {},
},
)
# 挂载所有 API 路由,路由前缀在 router.py 中统一定义
app.include_router(api_router)