FastAPI(27)- Handling Errors 处理错误 (下)

简介: FastAPI(27)- Handling Errors 处理错误 (下)

重写默认异常处理程序


  • FastAPI 有一些默认的异常处理程序
  • 比如:当引发 HTTPException 并且请求包含无效数据时,异常处理程序负责返回默认的 JSON 响应
  • 可以使用自己的异常处理程序覆盖(重写)这些默认的异常处理程序

 

重写 HTTPException 异常处理程序


# 导入对应的异常类
from fastapi.exceptions import HTTPException
from fastapi.responses import PlainTextResponse
# 重写 HTTPException 异常处理程序
@app.exception_handler(HTTPException)
async def http_exception_handler(request: Request, exc: HTTPException):
    # 原来是返回 JSONResponse,现在改成返回 PlainTextResponse
    return PlainTextResponse(content=exc.detail, status_code=exc.status_code)
@app.get("/items2/{item_id}")
async def read_item(item_id: int):
    if item_id == 3:
        # 抛出 HTTPException
        raise HTTPException(status_code=418, detail="Nope! I don't like 3.")
    return {"item_id": item_id}


item_id = 3 的请求结果

image.png

重写请求验证异常的处理程序


当请求包含无效数据时,FastAPI 会在内部引发 RequestValidationError,它还包括一个默认的异常处理程序

 

实际代码

# 需要先导入对应的异常类
from fastapi.exceptions import RequestValidationError
from fastapi.responses import PlainTextResponse
# 重写 RequestValidationError 异常处理程序
@app.exception_handler(RequestValidationError)
async def validation_exception_handler(request: Request, exc: RequestValidationError):
    # 返回自定义响应
    return PlainTextResponse(str(exc), status_code=status.HTTP_400_BAD_REQUEST)
@app.get("/items/{item_id}")
async def read_item(item_id: int):
    if item_id == 3:
        raise HTTPException(status_code=418, detail="Nope! I don't like 3.")
    return {"item_id": item_id}


怎么才会请求验证失败?

item_id 声明为 int,传一个无法转成 int 的字符串就会抛出 RequestValidationError,比如 "str"

 

在没有重写 RequestValidationError 异常处理程序前,请求验证失败的返回值

{
    "detail": [
        {
            "loc": [
                "path",
                "item_id"
            ],
            "msg": "value is not a valid integer",
            "type": "type_error.integer"
        }
    ]
}


按上面代码重写后,请求验证失败的返回值

1 validation error

path -> item_id

 value isnot a valid integer (type=type_error.integer)

 

使用 RequestValidationError 的 body 属性


RequestValidationError 包含它收到的带有无效数据的正文,可以在开发应用程序时使用它来记录主体并调试它,将其返回给用户

 

数据验证失败的请求结果

image.png

看一眼 RequestValidationError 的源码

image.png

有一个 body 实例属性

 

RequestValidationError vs ValidationError


  • RequestValidationError 是 Pydantic 的 ValidationError 的子类
  • 当使用了 response_model,如果响应数据校验失败,就会抛出 ValidationError
  • 客户端并不会直接收到 ValidationError,而是会收到 500,并报 Internal Server Error 服务器错误;这意味着就是服务端代码有问题
  • 正常来说,客户端看不到 ValidationError 是正确的,因为这可能会暴露安全漏洞

 

报错后,控制台输出

raise ValidationError(errors, field.type_)

pydantic.error_wrappers.ValidationError: 1 validation error for Item

response -> price

 value isnot a valid float (type=type_error.float)

 

FastAPI 的 HTTPException vs Starlette 的 HTTPException


  • FastAPI 的 HTTPException 是 Starlette 的 HTTPException 的子类
  • 唯一不同:FastAPI 的 HTTPException 支持自定义 Response Headers,在 OAuth2.0 中这是需要用到的
  • 但需要注册(重写/重用)一个异常处理程序时,应该用 Starlette 的 HTTPException 来注册它
  • 这样做的好处:当 Starlette 内部代码或扩展插件的任何部分引发 HTTPException,自己注册的异常处理程序都能捕获并处理它

 

重用 FastAPI HTTPException 的异常处理程序


重用、重写的区别

  • 重写:有点像覆盖的意思,把默认的功能完全改写
  • 重用:仍然会复用默认的功能,但会额外添加一些功能

 

实际代码
# 重用 HTTPException
from fastapi import FastAPI, HTTPException
# 为了重用,需要引入默认的 HTTPException、RequestValidationError 异常处理函数
from fastapi.exception_handlers import (
    http_exception_handler,
    request_validation_exception_handler,
)
from fastapi.exceptions import RequestValidationError
# 避免重名,所以 starlette 的 HTTPException 取一个别名
from starlette.exceptions import HTTPException as StarletteHTTPException
app = FastAPI()
# HTTPException 异常处理
@app.exception_handler(StarletteHTTPException)
async def custom_http_exception_handler(request, exc):
    print(f"OMG! An HTTP error!: {repr(exc)}")
    # 仍然会调用 默认的异常处理函数
    return await http_exception_handler(request, exc)
# RequestVlidationErrpr 异常处理
@app.exception_handler(RequestValidationError)
async def validation_exception_handler(request, exc):
    print(f"OMG! The client sent invalid data!: {exc}")
    # 仍然会调用 默认的异常处理函数
    return await request_validation_exception_handler(request, exc)
@app.get("/items/{item_id}")
async def read_item(item_id: int):
    if item_id == 3:
        raise HTTPException(status_code=418, detail="Nope! I don't like 3.")
    return {"item_id": item_id}


引发对应的异常后,控制台会输出

OMG! An HTTP error!: HTTPException(status_code=418, detail="Nope! I don't like 3.")
INFO:     127.0.0.1:57101 - "GET /items/3 HTTP/1.1" 418 I'm a Teapot
OMG! The client sent invalid data!: 1 validation error for Request
path -> item_id
  value is not a valid integer (type=type_error.integer)
INFO:     127.0.0.1:57119 - "GET /items/s HTTP/1.1" 422 Unprocessable Entity
相关文章
|
JSON API 数据格式
FastAPI(27)- Handling Errors 处理错误 (上)
FastAPI(27)- Handling Errors 处理错误 (上)
224 0
FastAPI(27)- Handling Errors 处理错误 (上)
|
JSON 开发框架 安全
FastAPI 学习之路(十九)处理错误
FastAPI 学习之路(十九)处理错误
FastAPI 学习之路(十九)处理错误
|
存储 JSON 数据格式
fastapi 请求文件 / 表单 / 处理错误 / 路径操作配置 / jsonable_encoder
fastapi 请求文件 / 表单 / 处理错误 / 路径操作配置 / jsonable_encoder
320 0
fastapi 请求文件 / 表单 / 处理错误 / 路径操作配置 / jsonable_encoder
|
NoSQL 测试技术 Redis
FastAPI(八十四)实战开发《在线课程学习系统》--接口测试(下)
FastAPI(八十四)实战开发《在线课程学习系统》--接口测试(下)
FastAPI(八十四)实战开发《在线课程学习系统》--接口测试(下)
|
测试技术 数据安全/隐私保护
FastAPI(八十四)实战开发《在线课程学习系统》--接口测试(上)
FastAPI(八十四)实战开发《在线课程学习系统》--接口测试(上)
|
存储 测试技术 数据安全/隐私保护
FastAPI(八十三)实战开发《在线课程学习系统》--注册接口单元测试
FastAPI(八十三)实战开发《在线课程学习系统》--注册接口单元测试
FastAPI(八十三)实战开发《在线课程学习系统》--注册接口单元测试
FastAPI(八十二)实战开发《在线课程学习系统》接口开发-- 课程上架下架
FastAPI(八十二)实战开发《在线课程学习系统》接口开发-- 课程上架下架
|
NoSQL Redis 数据库
FastAPI(八十一)实战开发《在线课程学习系统》接口开发-- 推荐课程列表与课程点赞
FastAPI(八十一)实战开发《在线课程学习系统》接口开发-- 推荐课程列表与课程点赞
FastAPI(八十)实战开发《在线课程学习系统》接口开发-- 课程列表
FastAPI(八十)实战开发《在线课程学习系统》接口开发-- 课程列表
FastAPI(七十九)实战开发《在线课程学习系统》接口开发-- 加入课程和退出课程
FastAPI(七十九)实战开发《在线课程学习系统》接口开发-- 加入课程和退出课程