mirror of
https://github.com/JefferyHcool/BiliNote.git
synced 2026-05-06 20:42:52 +08:00
- 实现 GroqTranscriber 类以支持 Groq 语音转录服务 - 新增异常处理中间件以提高系统稳定性 - 优化笔记生成流程,增加错误处理和日志记录 - 添加思维导图功能和相关组件 -重构 Markdown 查看器以支持切换视图模式
39 lines
1.5 KiB
Python
39 lines
1.5 KiB
Python
# 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))
|
|
)
|