Skip to main content

foxtive_supervisor/
error.rs

1//! Error types for the supervisor system
2
3use thiserror::Error;
4
5#[derive(Error, Debug)]
6pub enum SupervisorError {
7    #[error("Configuration error for task '{task_id}': {reason}")]
8    ConfigurationError { task_id: String, reason: String },
9
10    #[error("Dependency validation failed for task '{task_id}': {dependency_id} - {error:?}")]
11    DependencyValidation {
12        task_id: String,
13        dependency_id: String,
14        error: ValidationError,
15    },
16
17    #[error("Circular dependency detected between '{task_a}' and '{task_b}'")]
18    CircularDependency { task_a: String, task_b: String },
19
20    #[error("Prerequisite '{name}' failed: {error}")]
21    PrerequisiteFailed { name: String, error: String },
22
23    #[error("Task '{0}' not found")]
24    UnknownTask(String),
25
26    #[error("Internal error: {0}")]
27    InternalError(String),
28}
29
30#[derive(Debug, Clone, Copy, PartialEq, Eq)]
31pub enum ValidationError {
32    UnknownTaskId,
33    CircularDependency,
34}
35
36impl SupervisorError {
37    pub fn config(task_id: impl Into<String>, reason: impl Into<String>) -> Self {
38        Self::ConfigurationError {
39            task_id: task_id.into(),
40            reason: reason.into(),
41        }
42    }
43
44    pub fn dependency_validation(
45        task_id: &str,
46        dependency_id: &str,
47        error: ValidationError,
48    ) -> Self {
49        Self::DependencyValidation {
50            task_id: task_id.to_string(),
51            dependency_id: dependency_id.to_string(),
52            error,
53        }
54    }
55
56    pub fn circular_dependency(task_a: &str, task_b: &str) -> Self {
57        Self::CircularDependency {
58            task_a: task_a.to_string(),
59            task_b: task_b.to_string(),
60        }
61    }
62
63    pub fn prerequisite_failed(name: &'static str, error: anyhow::Error) -> Self {
64        Self::PrerequisiteFailed {
65            name: name.to_string(),
66            error: format!("{error:?}"),
67        }
68    }
69}