mirror of
https://github.com/JefferyHcool/BiliNote.git
synced 2026-07-20 04:02:38 +08:00
refactor(backend): 重构后端异常处理和模型管理
- 新增自定义异常类 BizException、NoteError 和 ProviderError - 优化了模型管理相关的逻辑,包括加载、删除和测试连接等功能 - 改进了 Douyin 下载器的错误处理 - 调整了任务重试逻辑和笔记生成的异常处理- 更新了相关组件和页面以适应新的异常处理机制
This commit is contained in:
@@ -1,38 +0,0 @@
|
||||
# app/core/exception_handlers.py
|
||||
from fastapi import Request, HTTPException
|
||||
from fastapi.exceptions import RequestValidationError
|
||||
from fastapi.responses import JSONResponse
|
||||
|
||||
from app.utils.logger import get_logger
|
||||
from app.utils.response import ResponseWrapper
|
||||
from app.utils.status_code import StatusCode
|
||||
logger = get_logger(__name__)
|
||||
|
||||
def register_exception_handlers(app):
|
||||
@app.exception_handler(RequestValidationError)
|
||||
async def validation_exception_handler(request: Request, exc: RequestValidationError):
|
||||
errors = []
|
||||
for err in exc.errors():
|
||||
loc = err.get("loc", [])
|
||||
field = loc[-1] if loc else "body"
|
||||
msg = err.get("msg", "参数不合法")
|
||||
errors.append({"field": field, "error": msg})
|
||||
return JSONResponse(
|
||||
status_code=400,
|
||||
content=ResponseWrapper.error(msg="参数验证失败", code=StatusCode.PARAM_ERROR, data=errors)
|
||||
)
|
||||
|
||||
@app.exception_handler(HTTPException)
|
||||
async def http_exception_handler(request: Request, exc: HTTPException):
|
||||
return JSONResponse(
|
||||
status_code=exc.status_code,
|
||||
content=ResponseWrapper.error(msg=str(exc.detail), code=StatusCode.FAIL)
|
||||
)
|
||||
|
||||
@app.exception_handler(Exception)
|
||||
async def global_exception_handler(request: Request, exc: Exception):
|
||||
logger.exception(f"服务器内部错误: {exc}")
|
||||
return JSONResponse(
|
||||
status_code=500,
|
||||
content=ResponseWrapper.error(msg="服务器内部错误", code=StatusCode.FAIL, data=str(exc))
|
||||
)
|
||||
Reference in New Issue
Block a user