use thiserror::Error;
#[derive(Debug, Error)]
pub enum LlmError {
#[error("HTTP transport error: {0}")]
Transport(#[from] reqwest::Error),
#[error("API error {status}: {body}")]
ApiError {
status: u16,
body: String,
},
#[error("response deserialisation failed: {source}\nbody: {body}")]
Deserialise {
source: serde_json::Error,
body: String,
},
#[error("missing configuration: {0}")]
MissingConfig(String),
#[error("Bedrock error: {0}")]
Bedrock(String),
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn api_error_display_includes_status_and_body() {
let err = LlmError::ApiError {
status: 429,
body: "rate limited".into(),
};
let s = err.to_string();
assert!(s.contains("429"), "status missing from: {s}");
assert!(s.contains("rate limited"), "body missing from: {s}");
}
#[test]
fn missing_config_display_includes_field_name() {
let err = LlmError::MissingConfig("OPENROUTER_API_KEY".into());
let s = err.to_string();
assert!(s.contains("OPENROUTER_API_KEY"), "field name missing: {s}");
}
#[test]
fn bedrock_error_display_includes_message() {
let err = LlmError::Bedrock("ValidationException: bad model id".into());
let s = err.to_string();
assert!(s.contains("ValidationException"), "message missing: {s}");
}
#[test]
fn deserialise_error_includes_body() {
let raw = "{invalid json}";
let serde_err = serde_json::from_str::<serde_json::Value>(raw).unwrap_err();
let err = LlmError::Deserialise {
source: serde_err,
body: raw.into(),
};
let s = err.to_string();
assert!(s.contains(raw), "raw body missing: {s}");
}
}