您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符
 
 
 
 

26 行
912 B

  1. from fastapi.encoders import jsonable_encoder
  2. from fastapi.exceptions import RequestValidationError
  3. from starlette.exceptions import HTTPException
  4. from starlette.requests import Request
  5. from starlette.responses import JSONResponse
  6. from starlette.status import HTTP_422_UNPROCESSABLE_ENTITY
  7. async def http_exception_handler(request: Request, exc: HTTPException) -> JSONResponse:
  8. headers = getattr(exc, "headers", None)
  9. if headers:
  10. return JSONResponse(
  11. {"detail": exc.detail}, status_code=exc.status_code, headers=headers
  12. )
  13. else:
  14. return JSONResponse({"detail": exc.detail}, status_code=exc.status_code)
  15. async def request_validation_exception_handler(
  16. request: Request, exc: RequestValidationError
  17. ) -> JSONResponse:
  18. return JSONResponse(
  19. status_code=HTTP_422_UNPROCESSABLE_ENTITY,
  20. content={"detail": jsonable_encoder(exc.errors())},
  21. )