1use std::io;
6use std::path::PathBuf;
7use thiserror::Error;
8
9pub type Result<T> = std::result::Result<T, Error>;
11
12#[derive(Error, Debug)]
14pub enum Error {
15 #[error("Configuration error: {0}")]
17 Config(String),
18
19 #[error("Template error in {template}: {message}")]
21 Template {
22 template: String,
24 message: String,
26 },
27
28 #[error("Template not found: {0}")]
30 TemplateNotFound(String),
31
32 #[error("Template validation failed for {template}: {reason}")]
34 TemplateValidation {
35 template: String,
37 reason: String,
39 },
40
41 #[error("Template execution failed: {0}")]
43 TemplateExecution(String),
44
45 #[error("Template compilation failed for {template}: {error}")]
47 TemplateCompilation {
48 template: PathBuf,
50 error: String,
52 },
53
54 #[error("Network error: {0}")]
56 Network(String),
57
58 #[error("HTTP request failed: {0}")]
60 HttpRequest(#[from] reqwest::Error),
61
62 #[error("Invalid target: {target} - {reason}")]
64 InvalidTarget {
65 target: String,
67 reason: String,
69 },
70
71 #[error("Target unreachable: {0}")]
73 TargetUnreachable(String),
74
75 #[error("Parse error: {0}")]
77 Parse(String),
78
79 #[error("YAML parse error: {0}")]
81 YamlParse(#[from] serde_yaml::Error),
82
83 #[error("JSON parse error: {0}")]
85 JsonParse(#[from] serde_json::Error),
86
87 #[error("IO error: {0}")]
89 Io(#[from] io::Error),
90
91 #[error("File not found: {0}")]
93 FileNotFound(PathBuf),
94
95 #[error("Plugin error in {plugin}: {message}")]
97 Plugin {
98 plugin: String,
100 message: String,
102 },
103
104 #[error("Plugin not found: {0}")]
106 PluginNotFound(String),
107
108 #[error("Scheduler error: {0}")]
110 Scheduler(String),
111
112 #[error("Operation timed out after {duration}")]
114 Timeout {
115 duration: String,
117 },
118
119 #[error("Execution error: {0}")]
121 Execution(String),
122
123 #[error("Command execution error: {0}")]
125 Command(String),
126
127 #[error("Rate limit exceeded: {0}")]
129 RateLimitExceeded(String),
130
131 #[error("Authentication failed: {0}")]
133 Authentication(String),
134
135 #[error("Authorization failed: {0}")]
137 Authorization(String),
138
139 #[error("Validation error: {0}")]
141 Validation(String),
142
143 #[error("Serialization error: {0}")]
145 Serialization(String),
146
147 #[error("Output format error: {format} - {message}")]
149 OutputFormat {
150 format: String,
152 message: String,
154 },
155
156 #[error("Protocol error for {protocol}: {message}")]
158 Protocol {
159 protocol: String,
161 message: String,
163 },
164
165 #[error("DNS resolution failed for {hostname}: {error}")]
167 DnsResolution {
168 hostname: String,
170 error: String,
172 },
173
174 #[error("TLS error: {0}")]
176 Tls(String),
177
178 #[error("Certificate error: {0}")]
180 Certificate(String),
181
182 #[error("Matcher error: {0}")]
184 Matcher(String),
185
186 #[error("Database error: {0}")]
188 Database(String),
189
190 #[error("Cache error: {0}")]
192 Cache(String),
193
194 #[error("Metrics error: {0}")]
196 Metrics(String),
197
198 #[error("Distributed system error: {0}")]
200 Distributed(String),
201
202 #[error("Worker {worker_id} error: {message}")]
204 Worker {
205 worker_id: String,
207 message: String,
209 },
210
211 #[error("Coordinator error: {0}")]
213 Coordinator(String),
214
215 #[error("AI error: {0}")]
217 Ai(String),
218
219 #[error("Internal error: {0}")]
221 Internal(String),
222
223 #[error("Update failed: {0}")]
225 Update(#[from] self_update::errors::Error),
226
227 #[error("Not implemented: {0}")]
229 NotImplemented(String),
230
231 #[error("{context}: {source}")]
233 WithContext {
234 context: String,
236 source: Box<Error>,
238 },
239
240 #[error("Multiple errors occurred: {}", .0.len())]
242 Multiple(Vec<Error>),
243}
244
245impl From<prometheus::Error> for Error {
247 fn from(err: prometheus::Error) -> Self {
248 Error::Metrics(err.to_string())
249 }
250}
251
252impl Error {
253 pub fn context<S: Into<String>>(self, context: S) -> Self {
255 Error::WithContext {
256 context: context.into(),
257 source: Box::new(self),
258 }
259 }
260
261 pub fn config<S: Into<String>>(message: S) -> Self {
263 Error::Config(message.into())
264 }
265
266 pub fn template<S: Into<String>, M: Into<String>>(template: S, message: M) -> Self {
268 Error::Template {
269 template: template.into(),
270 message: message.into(),
271 }
272 }
273
274 pub fn invalid_target<S: Into<String>, R: Into<String>>(target: S, reason: R) -> Self {
276 Error::InvalidTarget {
277 target: target.into(),
278 reason: reason.into(),
279 }
280 }
281
282 pub fn command<S: Into<String>>(message: S) -> Self {
284 Error::Command(message.into())
285 }
286
287 pub fn is_fatal(&self) -> bool {
289 matches!(self, Error::Internal(_) | Error::Coordinator(_))
290 }
291
292 pub fn is_retryable(&self) -> bool {
294 matches!(
295 self,
296 Error::Network(_)
297 | Error::TargetUnreachable(_)
298 | Error::Timeout { .. }
299 | Error::RateLimitExceeded(_)
300 | Error::HttpRequest(_)
301 )
302 }
303}
304
305pub trait ResultExt<T> {
307 fn context<C: Into<String>>(self, context: C) -> Result<T>;
309
310 fn with_context<C, F>(self, f: F) -> Result<T>
312 where
313 C: Into<String>,
314 F: FnOnce() -> C;
315}
316
317impl<T> ResultExt<T> for Result<T> {
318 fn context<C: Into<String>>(self, context: C) -> Result<T> {
319 self.map_err(|e| e.context(context))
320 }
321
322 fn with_context<C, F>(self, f: F) -> Result<T>
323 where
324 C: Into<String>,
325 F: FnOnce() -> C,
326 {
327 self.map_err(|e| e.context(f()))
328 }
329}
330
331#[cfg(test)]
332mod tests {
333 use super::*;
334
335 #[test]
336 fn test_error_context() {
337 let err = Error::Parse("invalid syntax".to_string());
338 let err_with_context = err.context("parsing configuration file");
339 assert!(matches!(err_with_context, Error::WithContext { .. }));
340 }
341
342 #[test]
343 fn test_error_is_retryable() {
344 assert!(Error::Network("connection reset".to_string()).is_retryable());
345 assert!(!Error::Internal("panic".to_string()).is_retryable());
346 }
347
348 #[test]
349 fn test_error_is_fatal() {
350 assert!(Error::Internal("critical failure".to_string()).is_fatal());
351 assert!(!Error::Network("timeout".to_string()).is_fatal());
352 }
353}