Skip to content

Commit 27ba70c

Browse files
committed
Expose llm call error codes
1 parent cbdf0fe commit 27ba70c

1 file changed

Lines changed: 29 additions & 2 deletions

File tree

src/llm.rs

Lines changed: 29 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
//!
33
//! Single HTTP provider (OpenRouter default). No streaming; minimal types.
44
5+
use std::error::Error;
56
use std::time::Duration;
67

78
use serde::{Deserialize, Serialize};
@@ -116,6 +117,32 @@ impl std::fmt::Display for LlmError {
116117

117118
impl std::error::Error for LlmError {}
118119

120+
/// Format a reqwest error with an explicit kind/code (timeout, connection, HTTP status) and the full
121+
/// error chain, so users see what actually happened.
122+
fn format_reqwest_error(e: &reqwest::Error) -> String {
123+
let code = if let Some(status) = e.status() {
124+
format!(
125+
"HTTP {} {}",
126+
status.as_u16(),
127+
status.canonical_reason().unwrap_or("")
128+
)
129+
} else if e.is_timeout() {
130+
"timeout".to_string()
131+
} else if e.is_connect() {
132+
"connection failed".to_string()
133+
} else {
134+
"request failed".to_string()
135+
};
136+
let mut detail = e.to_string();
137+
let mut src: Option<&(dyn Error + '_)> = e.source();
138+
while let Some(inner) = src {
139+
detail.push_str(" | ");
140+
detail.push_str(&inner.to_string());
141+
src = inner.source();
142+
}
143+
format!("{} | {}", code, detail)
144+
}
145+
119146
// --- Request/response (raw API shape for serde) ---
120147

121148
#[derive(Serialize)]
@@ -235,13 +262,13 @@ impl HttpProvider {
235262
.json(&body)
236263
.send()
237264
.await
238-
.map_err(|e| LlmError::Http(e.to_string()))?;
265+
.map_err(|e| LlmError::Http(format_reqwest_error(&e)))?;
239266

240267
let status = res.status();
241268
let text = res
242269
.text()
243270
.await
244-
.map_err(|e| LlmError::Http(e.to_string()))?;
271+
.map_err(|e| LlmError::Http(format_reqwest_error(&e)))?;
245272
if !status.is_success() {
246273
return Err(LlmError::Http(format!("{} {}", status, text)));
247274
}

0 commit comments

Comments
 (0)