许多情况下,需要向客户端返回一些特定的错误,比如
当 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)
在某些情况下,向 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]}
@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)
# 导入对应的异常类
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}
当请求包含无效数据时,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"
{
"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 实例属性
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)
# 重用 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