ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

FastAPI(27)- Handling Errors 处理错误

2022-01-05 13:07:17  阅读:189  来源: 互联网

标签:status exception 27 HTTPException FastAPI app item Errors id


FastAPI(27)- Handling Errors 处理错误 

 

前言

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

  • 客户端没有足够的权限进行该操作
  • 客户端无权访问该资源
  • 客户端尝试访问的项目不存在

 

HTTPException

介绍

  • 要将带有错误的 HTTP 响应(状态码和响应信息)返回给客户端,需要使用 HTTPException
  • HTTPException 是一个普通的 exception,包含和 API 相关的附加数据
  • 因为是一个 Python exception ,应该 raise 它,而不是 return 它

 

查看一下 HTTPException 源码

  • status_code:响应状态吗
  • detail:报错信息
  • headers:响应头

 

简单的栗子

当 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)

 

重点

  • 可以传递任何可以转换为 JSON 字符串的值给 detail 参数,而不仅仅是 str,可以是 dict、list
  • 它们由 FastAPI 自动处理并转换为 JSON

 

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

背景

  • 假设有一个自定义异常 UnicornException
  • 希望使用 FastAPI 全局处理此异常
  • 可以使用 @app.exception_handler() 添加自定义异常处理程序

 

实际代码

#!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,将会抛出 UnicornException,但它将由 unicorn_exception_handler 处理
  • JSONResponse 将会在后面的文章中详解

 

/unicorns/yolo 的请求结果

  

重写默认异常处理程序

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

 

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

当请求包含无效数据时,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

  • 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 is not 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

 

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

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有