FastAPI 印出422 unprocessable entity詳細資料

1
2
3
4
5
6
7
8
9
10
11
12
13
import logging
from fastapi import FastAPI, Request, status
from fastapi.exceptions import RequestValidationError
from fastapi.responses import JSONResponse

app = FastAPI()

@app.exception_handler(RequestValidationError)
async def validation_exception_handler(request: Request, exc: RequestValidationError):
exc_str = f'{exc}'.replace('\n', ' ').replace(' ', ' ')
logging.error(f"{request}: {exc_str}")
content = {'status_code': 10422, 'message': exc_str, 'data': None}
return JSONResponse(content=content, status_code=status.HTTP_422_UNPROCESSABLE_ENTITY)

參考:
https://github.com/tiangolo/fastapi/issues/3361#issuecomment-1002120988

FastAPI 印出422 unprocessable entity詳細資料

https://jenhaoyang.github.io/uncategorized/f59cb8110934/

Author

Steven

Posted on

2022-10-22

Updated on

2025-01-14

Licensed under

Comments