#[derive(Debug, thiserror::Error)]
#[non_exhaustive]
pub enum LlmWasmError {
#[error("Serialization error: {0}")]
Serialization(String),
#[error("Invalid configuration: {field}: {reason}")]
InvalidConfig {
field: String,
reason: String,
},
#[error("Request blocked by guard '{guard}': {reason}")]
GuardBlocked {
guard: String,
reason: String,
},
#[error("Retry budget exhausted after {attempts} attempts")]
RetryExhausted {
attempts: u32,
},
#[error("Cost budget exceeded: used ${used:.4}, limit ${limit:.4}")]
BudgetExceeded {
used: f64,
limit: f64,
},
#[error("Template render error: {0}")]
TemplateError(String),
#[error("No route matched for request")]
NoRouteMatched,
#[error("Cache error: {0}")]
CacheError(String),
#[error("Unknown model '{0}': no pricing data available")]
UnknownModel(String),
#[error("Provider error{}: {message}", kind_suffix(.kind))]
Provider {
kind: Option<String>,
message: String,
},
}
fn kind_suffix(kind: &Option<String>) -> String {
kind.as_deref()
.map(|k| format!(" ({k})"))
.unwrap_or_default()
}
impl From<serde_json::Error> for LlmWasmError {
fn from(e: serde_json::Error) -> Self {
LlmWasmError::Serialization(e.to_string())
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_serialization_error_display() {
let e = LlmWasmError::Serialization("bad json".into());
assert!(e.to_string().contains("bad json"));
}
#[test]
fn test_invalid_config_display() {
let e = LlmWasmError::InvalidConfig {
field: "temperature".into(),
reason: "must be in [0,2]".into(),
};
let s = e.to_string();
assert!(s.contains("temperature"));
assert!(s.contains("must be in [0,2]"));
}
#[test]
fn test_guard_blocked_display() {
let e = LlmWasmError::GuardBlocked {
guard: "content".into(),
reason: "blocklist hit".into(),
};
let s = e.to_string();
assert!(s.contains("content"));
assert!(s.contains("blocklist hit"));
}
#[test]
fn test_retry_exhausted_display() {
let e = LlmWasmError::RetryExhausted { attempts: 3 };
assert!(e.to_string().contains("3"));
}
#[test]
fn test_budget_exceeded_display() {
let e = LlmWasmError::BudgetExceeded {
used: 1.5,
limit: 1.0,
};
let s = e.to_string();
assert!(s.contains("1.5000"));
assert!(s.contains("1.0000"));
}
#[test]
fn test_template_error_display() {
let e = LlmWasmError::TemplateError("missing var".into());
assert!(e.to_string().contains("missing var"));
}
#[test]
fn test_no_route_matched_display() {
let e = LlmWasmError::NoRouteMatched;
assert!(e.to_string().contains("No route"));
}
#[test]
fn test_cache_error_display() {
let e = LlmWasmError::CacheError("expired".into());
assert!(e.to_string().contains("expired"));
}
#[test]
fn test_unknown_model_and_provider_display() {
assert_eq!(
LlmWasmError::UnknownModel("gpt-9".into()).to_string(),
"Unknown model 'gpt-9': no pricing data available"
);
let e = LlmWasmError::Provider {
kind: Some("overloaded_error".into()),
message: "Overloaded".into(),
};
assert_eq!(
e.to_string(),
"Provider error (overloaded_error): Overloaded"
);
let e = LlmWasmError::Provider {
kind: None,
message: "boom".into(),
};
assert_eq!(e.to_string(), "Provider error: boom");
}
#[test]
fn test_from_serde_json_error() {
let json_err = serde_json::from_str::<serde_json::Value>("{bad}").unwrap_err();
let e = LlmWasmError::from(json_err);
assert!(matches!(e, LlmWasmError::Serialization(_)));
}
}