|
@@ -1,4 +1,6 @@
|
|
|
-from fastapi import FastAPI, Request, status, BackgroundTasks, HTTPException
|
|
|
|
|
|
|
+from fastapi import FastAPI, Request, status
|
|
|
|
|
+from fastapi.exceptions import RequestValidationError
|
|
|
|
|
+from fastapi.responses import JSONResponse
|
|
|
import json
|
|
import json
|
|
|
from pydantic import BaseModel
|
|
from pydantic import BaseModel
|
|
|
from utils import Service
|
|
from utils import Service
|
|
@@ -6,6 +8,21 @@ import uvicorn
|
|
|
|
|
|
|
|
app = FastAPI()
|
|
app = FastAPI()
|
|
|
|
|
|
|
|
|
|
+# 添加全局异常处理器
|
|
|
|
|
+@app.exception_handler(RequestValidationError)
|
|
|
|
|
+async def validation_exception_handler(request: Request, exc: RequestValidationError):
|
|
|
|
|
+ return JSONResponse(
|
|
|
|
|
+ status_code=status.HTTP_400_BAD_REQUEST,
|
|
|
|
|
+ content={
|
|
|
|
|
+ "code": 400,
|
|
|
|
|
+ "msg": "请求参数错误",
|
|
|
|
|
+ "data": {
|
|
|
|
|
+ "detail": exc.errors(),
|
|
|
|
|
+ "body": exc.body
|
|
|
|
|
+ }
|
|
|
|
|
+ },
|
|
|
|
|
+ )
|
|
|
|
|
+
|
|
|
# 定义请求体
|
|
# 定义请求体
|
|
|
class KeyWordRequest(BaseModel):
|
|
class KeyWordRequest(BaseModel):
|
|
|
brand_name: str # 判定品牌名称
|
|
brand_name: str # 判定品牌名称
|