from fastapi import FastAPI, Request, File, HTTPException
import uvicorn
from datetime import datetime, time, date
app = FastAPI()
async def catch_exceptions_middleware(request: Request, call_next):
try:
return await call_next(request)
except Exception as e:
# you probably want some kind of logging here
session.close()
return JSONResponse(content={'data': None, 'status': '0', 'text': e.args, 'token': None}, status_code=500)
# 全局异常处理中间件
app.middleware('http')(catch_exceptions_middleware)
@app.get(path="/capacity_energy", summary='根据时间区间查询产能和能耗', tags=['产能与能耗'])
async def capacity_energy(start_time: datetime, end_time: datetime):
pass
fastapi全局异常处理
最新推荐文章于 2024-01-21 17:48:17 发布