异常日志完善

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,5 +1,6 @@
from __future__ import annotations
import logging
import os
from typing import Any, Dict, List
@ -10,6 +11,9 @@ from app.models import LLMChoice, LLMMessage, LLMProvider, LLMRequest, LLMRespon
from app.providers.base import LLMProviderClient
logger = logging.getLogger(__name__)
class OpenRouterProvider(LLMProviderClient):
name = LLMProvider.OPENROUTER.value
api_key_env = "OPENROUTER_API_KEY"
@ -51,7 +55,19 @@ class OpenRouterProvider(LLMProviderClient):
try:
response = await client.post(self.base_url, json=payload, headers=headers)
response.raise_for_status()
except httpx.HTTPStatusError as exc:
status_code = exc.response.status_code
body = exc.response.text
logger.error(
"OpenRouter upstream returned %s: %s", status_code, body, exc_info=True
)
raise ProviderAPICallError(
f"OpenRouter request failed with status {status_code}",
status_code=status_code,
response_text=body,
) from exc
except httpx.HTTPError as exc:
logger.error("OpenRouter transport error: %s", exc, exc_info=True)
raise ProviderAPICallError(f"OpenRouter request failed: {exc}") from exc
data: Dict[str, Any] = response.json()