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

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

前言


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

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

 

HTTPException


介绍

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

 

查看一下 HTTPException 源码

image.png

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

image.png

找不到 item_id 的请求结果


image.png


添加自定义 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 的请求结果

image.png


自定义 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 的请求结果

image.png

相关文章
|
JSON C++ 数据格式
FastAPI(27)- Handling Errors 处理错误 (下)
FastAPI(27)- Handling Errors 处理错误 (下)
493 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(七十九)实战开发《在线课程学习系统》接口开发-- 加入课程和退出课程