This commit is contained in:
Mikan
2026-06-21 02:41:48 +03:00
parent c21a13d2a3
commit bd85e186dc
31 changed files with 1372 additions and 319 deletions

View File

@@ -141,16 +141,36 @@ class LlmClient:
)
if resp.status_code >= 500:
raise LLMUnavailableError(
f"LLM provider returned {resp.status_code}: {resp.text[:200]}"
f"LLM provider returned HTTP {resp.status_code}: {resp.text[:300]}"
)
if resp.status_code == 429:
raise LLMUnavailableError("LLM provider rate-limited (429)")
raise LLMUnavailableError("LLM provider rate-limited (HTTP 429)")
if resp.status_code >= 400:
# Try to extract error message from JSON body
err_body = resp.text[:500]
try:
err_json = resp.json()
if "error" in err_json:
err_msg = err_json["error"].get("message", err_body)
else:
err_msg = err_body
except Exception: # noqa: BLE001
err_msg = err_body
raise LLMResponseError(
f"LLM provider returned {resp.status_code}: {resp.text[:500]}",
f"LLM provider returned HTTP {resp.status_code}: {err_msg}",
code="api_error",
)
data = resp.json()
# Parse JSON response — if this fails, the URL is likely wrong
# (pointing at an HTML page instead of an OpenAI-compatible API)
try:
data = resp.json()
except Exception as e: # noqa: BLE001
raise LLMResponseError(
f"LLM provider returned non-JSON response (check that "
f"api_url points to an OpenAI-compatible endpoint). "
f"First 200 chars: {resp.text[:200]}",
code="parse_error",
) from e
break
except (httpx.TimeoutException, asyncio.TimeoutError) as e:
last_exc = LLMTimeoutError(str(e))