FastAPI中间件,用于生成符合RFC-7807的错误响应
项目描述
fastapi-rfc7807
FastAPI中间件,将服务器端异常转换为符合RFC-7807的详细错误响应。
安装
fastapi_rfc7807
需要Python 3.6+
pip install fastapi_rfc7807
使用方法
以下是一个简单的示例,展示了配置FastAPI应用与fastapi_rfc7807
所需的最小设置。
from fastapi import FastAPI
from fastapi_rfc7807 import middleware
app = FastAPI()
middleware.register(app)
@app.get('/error')
async def error():
raise ValueError('something went wrong')
服务器返回的错误结果如下
$ curl localhost:8000/error
{"exc_type":"ValueError","type":"about:blank","title":"Unexpected Server Error","status":500,"detail":"something went wrong"}
请参阅示例目录以获取更多示例。