异常日志完善

This commit is contained in:
zhaoqingliang
2025-10-30 18:25:03 +08:00
parent 89af7cd0a4
commit 39911d78ab
10 changed files with 142 additions and 9 deletions

View File

@ -1,6 +1,7 @@
from __future__ import annotations
import asyncio
import logging
from contextlib import asynccontextmanager
import httpx
@ -17,6 +18,9 @@ from app.services import LLMGateway
from app.services.import_analysis import process_import_analysis_job
logger = logging.getLogger(__name__)
@asynccontextmanager
async def lifespan(app: FastAPI):
client = httpx.AsyncClient(timeout=httpx.Timeout(30.0))
@ -49,9 +53,18 @@ def create_app() -> FastAPI:
try:
return await gateway.chat(payload, client)
except ProviderConfigurationError as exc:
logger.error("Provider configuration error: %s", exc, exc_info=True)
raise HTTPException(status_code=422, detail=str(exc)) from exc
except ProviderAPICallError as exc:
raise HTTPException(status_code=502, detail=str(exc)) from exc
status_code = exc.status_code or 502
log_detail = exc.response_text or str(exc)
logger.error(
"Provider API call error (status %s): %s",
status_code,
log_detail,
exc_info=True,
)
raise HTTPException(status_code=status_code, detail=str(exc)) from exc
@application.post(
"/v1/import/analyze",