fix(ai): handle non-JSON error response in AI connection test
Replace res.json() with res.text() + fallback JSON parse so that non-JSON error bodies (e.g. HTTP 502 with empty body) no longer produce the unclear 'error decoding response body' message. Also add 502/503/504 to classify_error for proper category prefixes.
This commit is contained in:
1 parent
3a25487dea
commit
c026647785
1 file changed
+21
-4
@@ -964,8 +964,20 @@ pub async fn test_connection_core(config: &AiConfig) -> Result<AiTestConnectionR
|
|||||||
.await
|
.await
|
||||||
.map_err(|e| format!("AI request failed: {e}"))?;
|
.map_err(|e| format!("AI request failed: {e}"))?;
|
||||||
if !res.status().is_success() {
|
if !res.status().is_success() {
|
||||||
let data: serde_json::Value = res.json().await.map_err(|e| e.to_string())?;
|
let status = res.status();
|
||||||
return Err(categorize_error(&data, config));
|
let body = res.text().await.unwrap_or_default();
|
||||||
|
// Try JSON first (APIs like OpenAI return structured error bodies)
|
||||||
|
if let Ok(data) = serde_json::from_str::<serde_json::Value>(&body) {
|
||||||
|
let raw = extract_error(&data).unwrap_or_else(|| "API error".to_string());
|
||||||
|
return Err(format!("[{}] {}", classify_error(&raw), raw));
|
||||||
|
}
|
||||||
|
// Non-JSON body — show HTTP status + raw body
|
||||||
|
let msg = if body.trim().is_empty() {
|
||||||
|
format!("HTTP {}", status)
|
||||||
|
} else {
|
||||||
|
format!("HTTP {}: {}", status, body.trim())
|
||||||
|
};
|
||||||
|
return Err(format!("[{}] {}", classify_error(&msg), msg));
|
||||||
}
|
}
|
||||||
res.bytes_stream()
|
res.bytes_stream()
|
||||||
}
|
}
|
||||||
@@ -1005,9 +1017,14 @@ fn classify_error(msg: &str) -> &'static str {
|
|||||||
"modelNotFound"
|
"modelNotFound"
|
||||||
} else if lower.contains("429") || lower.contains("rate limit") || lower.contains("too many requests") {
|
} else if lower.contains("429") || lower.contains("rate limit") || lower.contains("too many requests") {
|
||||||
"rateLimit"
|
"rateLimit"
|
||||||
} else if lower.contains("timeout") || lower.contains("timed out") {
|
} else if lower.contains("timeout") || lower.contains("timed out") || lower.contains("504") {
|
||||||
"timeout"
|
"timeout"
|
||||||
} else if lower.contains("connect") || lower.contains("dns") || lower.contains("resolve") {
|
} else if lower.contains("connect")
|
||||||
|
|| lower.contains("dns")
|
||||||
|
|| lower.contains("resolve")
|
||||||
|
|| lower.contains("502")
|
||||||
|
|| lower.contains("503")
|
||||||
|
{
|
||||||
"network"
|
"network"
|
||||||
} else {
|
} else {
|
||||||
"unknown"
|
"unknown"
|
||||||
|
|||||||
Reference in new issue
Block a user