use serde::{Deserialize, Serialize};
use std::fmt;
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub enum ErrorKind {
InvalidInput,
Config,
Provider,
Tool,
Temporary,
NotFound,
Unknown,
}
impl ErrorKind {
pub fn as_str(&self) -> &'static str {
match self {
ErrorKind::InvalidInput => "invalid_input",
ErrorKind::Config => "config",
ErrorKind::Provider => "provider",
ErrorKind::Tool => "tool",
ErrorKind::Temporary => "temporary",
ErrorKind::NotFound => "not_found",
ErrorKind::Unknown => "unknown",
}
}
}
impl fmt::Display for ErrorKind {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.write_str(self.as_str())
}
}
#[derive(Debug, Clone, thiserror::Error)]
#[error("[{kind}] {message}")]
pub struct ConduitError {
pub kind: ErrorKind,
pub message: String,
#[source]
pub cause: Option<Box<ConduitError>>,
}
impl ConduitError {
pub fn new(kind: ErrorKind, message: impl Into<String>) -> Self {
Self {
kind,
message: message.into(),
cause: None,
}
}
pub fn with_cause(self, cause: ConduitError) -> Self {
Self {
kind: self.kind,
message: self.message,
cause: Some(Box::new(cause)),
}
}
}