fastapi处理tortoise-orm异常

fastapi处理tortoise-orm异常

  1. 将add_exception_handlers设置为false

register_tortoise(
                  add_exception_handlers=False)
  1. 捕捉异常

@app.exception_handler(BaseORMException)
async def business_exception_handler(request: Request, error: BaseORMException):
    return JSONResponse(
        status_code=status.HTTP_200_OK,
        content={
            "code": status.HTTP_500_INTERNAL_SERVER_ERROR,
            "message": "服务器错误"
        }
    )

Last updated