From c0266477854541c8f4207889e1fc228e5f43c903 Mon Sep 17 00:00:00 2001 From: lexmin0412 Date: Tue, 23 Jun 2026 12:04:46 +0800 Subject: [PATCH] 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. --- crates/dbx-core/src/ai.rs | 25 +++++++++++++++++++++---- 1 file changed, 21 insertions(+), 4 deletions(-) diff --git a/crates/dbx-core/src/ai.rs b/crates/dbx-core/src/ai.rs index 9fdd7f737..b0282259a 100644 --- a/crates/dbx-core/src/ai.rs +++ b/crates/dbx-core/src/ai.rs @@ -964,8 +964,20 @@ pub async fn test_connection_core(config: &AiConfig) -> Result(&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() } @@ -1005,9 +1017,14 @@ fn classify_error(msg: &str) -> &'static str { "modelNotFound" } else if lower.contains("429") || lower.contains("rate limit") || lower.contains("too many requests") { "rateLimit" - } else if lower.contains("timeout") || lower.contains("timed out") { + } else if lower.contains("timeout") || lower.contains("timed out") || lower.contains("504") { "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" } else { "unknown"