use serde::{Deserialize, Serialize};
use thiserror::Error;
use crate::throttle::Throttle;
pub type Result<T> = std::result::Result<T, TenxError>;
#[derive(Error, Debug, Serialize, Deserialize, Clone, Eq, PartialEq)]
pub enum TenxError {
#[error("config error: {0}")]
Config(String),
#[error("Io error: {0}")]
Io(String),
#[error("Model error: {0}")]
Model(String),
#[error("{msg}: {path}")]
NotFound { msg: String, path: String },
#[error("Error parsing response from model: {user}")]
ResponseParse {
user: String,
model: String,
},
#[error("Error resolving context: {0}")]
Resolve(String),
#[error("{0}")]
SessionStore(String),
#[error("Internal error: {0}")]
Internal(String),
#[error("Error applying patch: {user}")]
Patch { user: String, model: String },
#[error("{name}: {user}")]
Check {
name: String,
user: String,
model: String,
},
#[error("Error sending event: {0}")]
EventSend(String),
#[error("Error executing command: {cmd}")]
Exec { cmd: String, error: String },
#[error("Throttled: {0}")]
Throttle(Throttle),
#[error("Max retries exceeded: {0}")]
MaxRetries(u64),
}
impl TenxError {
pub fn should_retry(&self) -> Option<String> {
match self {
TenxError::Check { model, .. } => Some(model.to_string()),
TenxError::Patch { model, .. } => Some(model.to_string()),
TenxError::ResponseParse { model, .. } => Some(model.to_string()),
_ => None,
}
}
}
impl From<std::io::Error> for TenxError {
fn from(error: std::io::Error) -> Self {
TenxError::Io(error.to_string())
}
}
impl From<misanthropy::Error> for TenxError {
fn from(error: misanthropy::Error) -> Self {
match error {
misanthropy::Error::RateLimitExceeded(_msg) => TenxError::Throttle(Throttle::Backoff),
misanthropy::Error::ApiOverloaded(_msg) => TenxError::Throttle(Throttle::Backoff),
_ => TenxError::Model(error.to_string()),
}
}
}