Add HTTP exception helpers and update models
Introduced utils/http/http_exceptions.py with common HTTP exception helpers for FastAPI. Updated main.py to use a global exception handler that logs and hides internal errors. Refined models/README.md to document new models and relationships, including PhysicalFile and UploadSession, and updated DTO and enum documentation. Simplified ThemeResponse in models/color.py. Improved models/download.py with type annotations, index changes, and import optimizations. Fixed a parameter type in clean.py. Co-Authored-By: 砂糖橘 <54745033+Foxerine@users.noreply.github.com>
This commit is contained in:
22
main.py
22
main.py
@@ -1,11 +1,15 @@
|
||||
from fastapi import FastAPI
|
||||
from typing import NoReturn
|
||||
|
||||
from fastapi import FastAPI, Request
|
||||
|
||||
from utils.conf import appmeta
|
||||
from utils.http.http_exceptions import raise_internal_error
|
||||
from utils.lifespan import lifespan
|
||||
from models.database import init_db
|
||||
from models.migration import migration
|
||||
from utils.JWT import JWT
|
||||
from routers import router
|
||||
from loguru import logger as l
|
||||
|
||||
# 添加初始化数据库启动项
|
||||
lifespan.add_startup(init_db)
|
||||
@@ -24,11 +28,21 @@ app = FastAPI(
|
||||
debug=appmeta.debug,
|
||||
)
|
||||
|
||||
@app.exception_handler(Exception)
|
||||
async def handle_unexpected_exceptions(request: Request, exc: Exception) -> NoReturn:
|
||||
"""
|
||||
捕获所有未经处理的fastapi异常,防止敏感信息泄露。
|
||||
"""
|
||||
l.exception(exc)
|
||||
l.error(f"An unhandled exception occurred for request: {request.method} {request.url.path}")
|
||||
|
||||
raise_internal_error()
|
||||
|
||||
# 挂载路由
|
||||
app.include_router(router)
|
||||
|
||||
# 防止直接运行 main.py
|
||||
if __name__ == "__main__":
|
||||
from loguru import logger
|
||||
logger.error("请用 fastapi ['dev', 'run'] 命令启动服务")
|
||||
exit(1)
|
||||
l.error("请用 fastapi ['dev', 'run'] 命令启动服务")
|
||||
exit(1)
|
||||
|
||||
Reference in New Issue
Block a user