其他分享
首页 > 其他分享> > FastAPI(27)- Handling Errors 处理错误

FastAPI(27)- Handling Errors 处理错误

作者:互联网

FastAPI(27)- Handling Errors 处理错误 

 

前言

许多情况下,需要向客户端返回一些特定的错误,比如

 

HTTPException

介绍

 

查看一下 HTTPException 源码

 

简单的栗子

当 item_id 不存在的时候,则抛出 404 错误码

#!usr/bin/env python
# -*- coding:utf-8 _*-
"""
# author: 小菠萝测试笔记
# blog:  https://www.cnblogs.com/poloyy/
# time: 2021/9/22 9:52 上午
# file: 21_File.py
"""
import uvicorn
from fastapi import FastAPI, HTTPException, status

app = FastAPI()

items = {"foo": "The Foo Wrestlers"}


@app.get("/items/{item_id}")
async def read_item(item_id: str):
    if item_id not in items:
        raise HTTPException(status_code=status.HTTP_404_NOT_FOUND, detail="item_id 不存在")

    return {"item": items[item_id]}


if __name__ == "__main__":
    uvicorn.run(app="23_handle_error:app", host="127.0.0.1", port=8080, reload=True, debug=True)

 

重点

 

item_id = foo 的请求结果

 

找不到 item_id 的请求结果


添加自定义 Headers

在某些情况下,向 HTTP 错误添加自定义 Headers 会挺有用的

@app.get("/items-header/{item_id}")
async def read_item_header(item_id: str):
    if item_id not in items:
        raise HTTPException(
            status_code=status.HTTP_404_NOT_FOUND,
            detail="Item not found",
            headers={"X-Error": "There goes my error"},
        )
    return {"item": items[item_id]}

 

找不到 item_id 的请求结果

 

自定义 Exception Handlers

背景

 

实际代码

#!usr/bin/env python
# -*- coding:utf-8 _*-
"""
# author: 小菠萝测试笔记
# blog:  https://www.cnblogs.com/poloyy/
# time: 2021/9/22 9:52 上午
# file: 21_File.py
"""
import uvicorn
from fastapi import FastAPI, HTTPException, status, Request
from fastapi.responses import JSONResponse

app = FastAPI()


class UnicornException(Exception):
    def __init__(self, name: str):
        self.name = name


@app.exception_handler(UnicornException)
async def unicorn_exception_handler(request: Request, exc: UnicornException):
    return JSONResponse(
        status_code=status.HTTP_418_IM_A_TEAPOT,
        content={"message": f"Oops! {exc.name} did something. "},
    )


@app.get("/unicorns/{name}")
async def read_unicorn(name: str):
    if name == "yolo":
        raise UnicornException(name=name)
    return {"unicorn_name": name}


if __name__ == "__main__":
    uvicorn.run(app="23_handle_error:app", host="127.0.0.1", port=8080, reload=True, debug=True)

 

/unicorns/yolo 的请求结果

  

重写默认异常处理程序

 

重写 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 的请求结果

 

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

当请求包含无效数据时,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 is not a valid integer (type=type_error.integer)

 

使用 RequestValidationError 的 body 属性

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

 

数据验证失败的请求结果

 

看一眼 RequestValidationError 的源码

有一个 body 实例属性

 

RequestValidationError vs ValidationError

 

报错后,控制台输出

    raise ValidationError(errors, field.type_)
pydantic.error_wrappers.ValidationError: 1 validation error for Item
response -> price
  value is not a valid float (type=type_error.float)

 

FastAPI 的 HTTPException vs 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

 

标签:status,exception,27,HTTPException,FastAPI,app,item,Errors,id
来源: https://www.cnblogs.com/xiao-xue-di/p/15766415.html