use thiserror::Error;
#[derive(Debug, Error)]
pub enum LlmError {
#[error("llm circuit breaker is open; failing fast")]
CircuitOpen,
#[error("llm transport error: {0}")]
Transport(#[from] reqwest::Error),
#[error("llm api error: status {status}, body: {body}")]
Api {
status: u16,
body: String,
},
#[error("llm response decode error: {0}")]
Decode(#[from] serde_json::Error),
#[error("llm stream protocol error: {0}")]
StreamProtocol(String),
#[error("llm stream utf-8 error: {0}")]
InvalidUtf8(#[from] std::string::FromUtf8Error),
#[error("llm failed after {attempts} attempts; last error: {source}")]
AllRetriesFailed {
attempts: u32,
#[source]
source: Box<LlmError>,
},
}
impl LlmError {
pub fn is_retryable(&self) -> bool {
match self {
LlmError::Transport(_) | LlmError::StreamProtocol(_) | LlmError::InvalidUtf8(_) => true,
LlmError::Api { status, .. } => {
*status == 408 || *status == 429 || (500..600).contains(status)
}
LlmError::CircuitOpen | LlmError::Decode(_) | LlmError::AllRetriesFailed { .. } => {
false
}
}
}
}
pub type Result<T> = std::result::Result<T, LlmError>;