's'
This commit is contained in:
12
app/main.py
12
app/main.py
@@ -15,8 +15,9 @@
|
||||
"""
|
||||
import logging
|
||||
|
||||
from fastapi import FastAPI
|
||||
from fastapi import FastAPI, Request, HTTPException
|
||||
from fastapi.middleware.cors import CORSMiddleware
|
||||
from fastapi.responses import JSONResponse
|
||||
|
||||
from .routers import (
|
||||
nomadvip_router,
|
||||
@@ -41,6 +42,15 @@ app.add_middleware(
|
||||
allow_headers=["*"],
|
||||
)
|
||||
|
||||
|
||||
@app.exception_handler(Exception)
|
||||
async def global_exception_handler(request: Request, exc: Exception):
|
||||
"""捕获未处理异常,记录日志后返回 500(HTTPException 交由 FastAPI 默认处理)"""
|
||||
if isinstance(exc, HTTPException):
|
||||
raise exc
|
||||
logging.error(f"未处理异常: {request.url.path} - {exc}", exc_info=True)
|
||||
return JSONResponse(status_code=500, content={"detail": str(exc)})
|
||||
|
||||
# 日志
|
||||
logging.basicConfig(
|
||||
level=logging.INFO,
|
||||
|
||||
Reference in New Issue
Block a user