FastAPI 异常处理

HTTPException

HTTPException 是 FastAPI 内置的异常类,用于中断正常处理流程并返回标准的 HTTP 错误响应。适用客户端错误(4xx)。

python
from fastapi import FastAPI, HTTPException

app = FastAPI()

@app.get('/news/{id}')
async def get_news(id: int):
    id_list = [1, 2, 3, 4, 5, 6]
    if id not in id_list:
        raise HTTPException(status_code=404, detail="当前id不存在")
    return {"id": id}

请求 /news/100 响应:{"detail": "当前id不存在"}(404)。

参数

参数类型必填说明
status_codeint✅HTTP 状态码(404、401、403 等)
detailstr / Any❌错误详情,序列化为 JSON 响应
headersdict❌自定义响应头

常用状态码

场景状态码
资源未找到404
认证失败401
权限不足403
参数错误422
请求参数有误400

HTTPException vs Pydantic 自动校验

特性HTTPExceptionPydantic 自动校验
触发方式手动 raise自动触发
使用场景业务逻辑错误(ID 不存在)参数类型/格式错误
状态码自定义(通常 4xx)默认 422

进阶:自定义异常处理

python
from fastapi import FastAPI, Request, HTTPException
from fastapi.responses import JSONResponse

@app.exception_handler(HTTPException)
async def custom_http_exception_handler(request: Request, exc: HTTPException):
    return JSONResponse(
        status_code=exc.status_code,
        content={"success": False, "error_code": f"ERR_{exc.status_code}", "message": exc.detail},
    )

要点

客户端出错用 HTTPException,自动返回标准 JSON,status_code + detail 两参数。