eshanized_polaris_core/
errors.rs

1//! Error types for Polaris.
2//!
3//! This module defines the error hierarchy used throughout the framework.
4
5use std::fmt;
6
7/// The main error type for Polaris operations.
8#[derive(Debug, thiserror::Error)]
9#[non_exhaustive]
10pub enum PolarisError {
11    /// Error connecting to cluster
12    #[error("Failed to connect to cluster: {0}")]
13    ConnectionFailed(String),
14
15    /// Error during task scheduling
16    #[error("Task scheduling failed: {0}")]
17    SchedulingFailed(String),
18
19    /// Task execution error
20    #[error("Task execution failed: {0}")]
21    TaskExecutionFailed(String),
22
23    /// Task timeout
24    #[error("Task timed out after {0:?}")]
25    TaskTimeout(std::time::Duration),
26
27    /// Node not found
28    #[error("Node not found: {0}")]
29    NodeNotFound(String),
30
31    /// Node unavailable
32    #[error("Node unavailable: {0}")]
33    NodeUnavailable(String),
34
35    /// Invalid configuration
36    #[error("Invalid configuration: {0}")]
37    InvalidConfig(String),
38
39    /// Serialization error
40    #[error("Serialization error: {0}")]
41    SerializationError(String),
42
43    /// Network error
44    #[error("Network error: {0}")]
45    NetworkError(String),
46
47    /// Storage error
48    #[error("Storage error: {0}")]
49    StorageError(String),
50
51    /// Authentication error
52    #[error("Authentication failed: {0}")]
53    AuthenticationFailed(String),
54
55    /// Authorization error
56    #[error("Authorization failed: {0}")]
57    AuthorizationFailed(String),
58
59    /// Resource limit exceeded
60    #[error("Resource limit exceeded: {0}")]
61    ResourceLimitExceeded(String),
62
63    /// DAG cycle detected
64    #[error("Cycle detected in task dependency graph")]
65    DagCycleDetected,
66
67    /// Invalid task state transition
68    #[error("Invalid task state transition from {from} to {to}")]
69    InvalidStateTransition {
70        /// Current state
71        from: String,
72        /// Attempted new state
73        to: String,
74    },
75
76    /// IO error
77    #[error("IO error: {0}")]
78    Io(#[from] std::io::Error),
79
80    /// Other error
81    #[error("{0}")]
82    Other(String),
83}
84
85/// Result type alias for Polaris operations.
86pub type PolarisResult<T> = Result<T, PolarisError>;
87
88impl PolarisError {
89    /// Create a connection error
90    pub fn connection_failed(msg: impl fmt::Display) -> Self {
91        Self::ConnectionFailed(msg.to_string())
92    }
93
94    /// Create a scheduling error
95    pub fn scheduling_failed(msg: impl fmt::Display) -> Self {
96        Self::SchedulingFailed(msg.to_string())
97    }
98
99    /// Create a task execution error
100    pub fn task_execution_failed(msg: impl fmt::Display) -> Self {
101        Self::TaskExecutionFailed(msg.to_string())
102    }
103
104    /// Create a network error
105    pub fn network_error(msg: impl fmt::Display) -> Self {
106        Self::NetworkError(msg.to_string())
107    }
108
109    /// Create a storage error
110    pub fn storage_error(msg: impl fmt::Display) -> Self {
111        Self::StorageError(msg.to_string())
112    }
113
114    /// Create an other error
115    pub fn other(msg: impl fmt::Display) -> Self {
116        Self::Other(msg.to_string())
117    }
118}
119
120#[cfg(test)]
121mod tests {
122    use super::*;
123
124    #[test]
125    fn test_error_display() {
126        let err = PolarisError::connection_failed("test connection");
127        assert!(err.to_string().contains("test connection"));
128    }
129
130    #[test]
131    fn test_error_from_io() {
132        let io_err = std::io::Error::new(std::io::ErrorKind::NotFound, "file not found");
133        let err: PolarisError = io_err.into();
134        assert!(matches!(err, PolarisError::Io(_)));
135    }
136}