feat: 引入 Loguru

This commit is contained in:
2026-09-29 00:47:57 +08:00
parent 0fdde8c3e6
commit 437fcd74b1
8 changed files with 139 additions and 31 deletions
+7 -4
View File
@@ -1,19 +1,17 @@
"""业务异常与全局 API 异常处理"""
import logging
from http import HTTPStatus
from fastapi import FastAPI, Request
from fastapi.exceptions import RequestValidationError
from fastapi.responses import JSONResponse
from loguru import logger
from openai import APIError, APITimeoutError
from starlette.exceptions import HTTPException
from app.common.response import error_content
from app.exceptions import AppError, DecisionError, ImportProcessError
logger = logging.getLogger(__name__)
class BizException(AppError):
"""可安全返回给调用方的业务错误"""
@@ -54,7 +52,12 @@ async def handle_unexpected_exception(
_request: Request, exc: Exception
) -> JSONResponse:
# 详细异常仅记录在服务端,不将堆栈或内部错误内容返回给前端
logger.error("未处理的服务器异常", exc_info=(type(exc), exc, exc.__traceback__))
logger.bind(thread_id=getattr(exc, "_agent_thread_id", "-")).opt(
exception=exc
).error("未处理的服务器异常")
exc._logged_by_api = True
# 节点包装异常时沿原始原因查找,区分上游服务故障与内部错误
cause = exc
while isinstance(cause, ImportProcessError):