use std::io;
use std::path::PathBuf;
use thiserror::Error;
pub type Result<T> = std::result::Result<T, Error>;
#[derive(Error, Debug)]
pub enum Error {
#[error("Configuration error: {0}")]
Config(String),
#[error("Template error in {template}: {message}")]
Template {
template: String,
message: String,
},
#[error("Template not found: {0}")]
TemplateNotFound(String),
#[error("Template validation failed for {template}: {reason}")]
TemplateValidation {
template: String,
reason: String,
},
#[error("Template execution failed: {0}")]
TemplateExecution(String),
#[error("Template compilation failed for {template}: {error}")]
TemplateCompilation {
template: PathBuf,
error: String,
},
#[error("Network error: {0}")]
Network(String),
#[error("HTTP request failed: {0}")]
HttpRequest(#[from] reqwest::Error),
#[error("Invalid target: {target} - {reason}")]
InvalidTarget {
target: String,
reason: String,
},
#[error("Target unreachable: {0}")]
TargetUnreachable(String),
#[error("Parse error: {0}")]
Parse(String),
#[error("YAML parse error: {0}")]
YamlParse(#[from] serde_yaml::Error),
#[error("JSON parse error: {0}")]
JsonParse(#[from] serde_json::Error),
#[error("IO error: {0}")]
Io(#[from] io::Error),
#[error("File not found: {0}")]
FileNotFound(PathBuf),
#[error("Plugin error in {plugin}: {message}")]
Plugin {
plugin: String,
message: String,
},
#[error("Plugin not found: {0}")]
PluginNotFound(String),
#[error("Scheduler error: {0}")]
Scheduler(String),
#[error("Operation timed out after {duration}")]
Timeout {
duration: String,
},
#[error("Execution error: {0}")]
Execution(String),
#[error("Command execution error: {0}")]
Command(String),
#[error("Rate limit exceeded: {0}")]
RateLimitExceeded(String),
#[error("Authentication failed: {0}")]
Authentication(String),
#[error("Authorization failed: {0}")]
Authorization(String),
#[error("Validation error: {0}")]
Validation(String),
#[error("Serialization error: {0}")]
Serialization(String),
#[error("Output format error: {format} - {message}")]
OutputFormat {
format: String,
message: String,
},
#[error("Protocol error for {protocol}: {message}")]
Protocol {
protocol: String,
message: String,
},
#[error("DNS resolution failed for {hostname}: {error}")]
DnsResolution {
hostname: String,
error: String,
},
#[error("TLS error: {0}")]
Tls(String),
#[error("Certificate error: {0}")]
Certificate(String),
#[error("Matcher error: {0}")]
Matcher(String),
#[error("Database error: {0}")]
Database(String),
#[error("Cache error: {0}")]
Cache(String),
#[error("Metrics error: {0}")]
Metrics(String),
#[error("Distributed system error: {0}")]
Distributed(String),
#[error("Worker {worker_id} error: {message}")]
Worker {
worker_id: String,
message: String,
},
#[error("Coordinator error: {0}")]
Coordinator(String),
#[error("AI error: {0}")]
Ai(String),
#[error("Internal error: {0}")]
Internal(String),
#[error("Update failed: {0}")]
Update(#[from] self_update::errors::Error),
#[error("Not implemented: {0}")]
NotImplemented(String),
#[error("{context}: {source}")]
WithContext {
context: String,
source: Box<Error>,
},
#[error("Multiple errors occurred: {}", .0.len())]
Multiple(Vec<Error>),
}
impl From<prometheus::Error> for Error {
fn from(err: prometheus::Error) -> Self {
Error::Metrics(err.to_string())
}
}
impl Error {
pub fn context<S: Into<String>>(self, context: S) -> Self {
Error::WithContext {
context: context.into(),
source: Box::new(self),
}
}
pub fn config<S: Into<String>>(message: S) -> Self {
Error::Config(message.into())
}
pub fn template<S: Into<String>, M: Into<String>>(template: S, message: M) -> Self {
Error::Template {
template: template.into(),
message: message.into(),
}
}
pub fn invalid_target<S: Into<String>, R: Into<String>>(target: S, reason: R) -> Self {
Error::InvalidTarget {
target: target.into(),
reason: reason.into(),
}
}
pub fn command<S: Into<String>>(message: S) -> Self {
Error::Command(message.into())
}
pub fn is_fatal(&self) -> bool {
matches!(self, Error::Internal(_) | Error::Coordinator(_))
}
pub fn is_retryable(&self) -> bool {
matches!(
self,
Error::Network(_)
| Error::TargetUnreachable(_)
| Error::Timeout { .. }
| Error::RateLimitExceeded(_)
| Error::HttpRequest(_)
)
}
}
pub trait ResultExt<T> {
fn context<C: Into<String>>(self, context: C) -> Result<T>;
fn with_context<C, F>(self, f: F) -> Result<T>
where
C: Into<String>,
F: FnOnce() -> C;
}
impl<T> ResultExt<T> for Result<T> {
fn context<C: Into<String>>(self, context: C) -> Result<T> {
self.map_err(|e| e.context(context))
}
fn with_context<C, F>(self, f: F) -> Result<T>
where
C: Into<String>,
F: FnOnce() -> C,
{
self.map_err(|e| e.context(f()))
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_error_context() {
let err = Error::Parse("invalid syntax".to_string());
let err_with_context = err.context("parsing configuration file");
assert!(matches!(err_with_context, Error::WithContext { .. }));
}
#[test]
fn test_error_is_retryable() {
assert!(Error::Network("connection reset".to_string()).is_retryable());
assert!(!Error::Internal("panic".to_string()).is_retryable());
}
#[test]
fn test_error_is_fatal() {
assert!(Error::Internal("critical failure".to_string()).is_fatal());
assert!(!Error::Network("timeout".to_string()).is_fatal());
}
}