use thiserror::Error;
#[derive(Debug, Clone, Error)]
pub enum LlmError {
#[error("LLM transport failed{}: {message}", status.map(|c| format!(" (HTTP {c})")).unwrap_or_default())]
Transport {
status: Option<u16>,
message: String,
},
#[error("LLM response was unparseable: {0}")]
Unparseable(String),
#[error("{message}")]
ModelStopped { finish: String, message: String },
#[error("LLM not configured: {0}")]
NotConfigured(String),
#[error("LLM backend {kind}: {message}")]
Backend {
kind: BackendErrorKind,
message: String,
},
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum BackendErrorKind {
Contract,
Authentication,
UsageLimit,
Request,
UnknownExit,
}
impl std::fmt::Display for BackendErrorKind {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.write_str(match self {
Self::Contract => "contract failure",
Self::Authentication => "authentication failure",
Self::UsageLimit => "usage limit",
Self::Request => "request rejection",
Self::UnknownExit => "failure",
})
}
}
impl BackendErrorKind {
pub fn as_str(self) -> &'static str {
match self {
Self::Contract => "contract",
Self::Authentication => "authentication",
Self::UsageLimit => "usage_limit",
Self::Request => "request",
Self::UnknownExit => "unknown_exit",
}
}
}
impl LlmError {
pub fn status(&self) -> Option<u16> {
match self {
Self::Transport { status, .. } => *status,
Self::Unparseable(_)
| Self::ModelStopped { .. }
| Self::NotConfigured(_)
| Self::Backend { .. } => None,
}
}
}