ruvswarm_core/
error.rs

1//! Error types for the swarm orchestration system
2
3use core::fmt;
4
5#[cfg(not(feature = "std"))]
6use alloc::string::String;
7
8#[cfg(feature = "std")]
9use thiserror::Error;
10
11/// Result type alias for swarm operations
12pub type Result<T> = core::result::Result<T, SwarmError>;
13
14/// Core error types for swarm orchestration
15#[derive(Debug, Clone, PartialEq, Eq)]
16#[cfg_attr(feature = "std", derive(Error))]
17pub enum SwarmError {
18    /// Agent not found in registry
19    #[cfg_attr(feature = "std", error("Agent not found: {id}"))]
20    AgentNotFound {
21        /// The unique identifier of the agent that was not found
22        id: String,
23    },
24
25    /// Task execution failed
26    #[cfg_attr(feature = "std", error("Task execution failed: {reason}"))]
27    TaskExecutionFailed {
28        /// Description of why the task execution failed
29        reason: String,
30    },
31
32    /// Invalid swarm topology
33    #[cfg_attr(feature = "std", error("Invalid topology: {reason}"))]
34    InvalidTopology {
35        /// Description of why the topology is invalid
36        reason: String,
37    },
38
39    /// Communication error between agents
40    #[cfg_attr(feature = "std", error("Communication error: {reason}"))]
41    CommunicationError {
42        /// Description of the communication error
43        reason: String,
44    },
45
46    /// Resource exhaustion
47    #[cfg_attr(feature = "std", error("Resource exhausted: {resource}"))]
48    ResourceExhausted {
49        /// The type of resource that was exhausted (e.g., "memory", "cpu", "connections")
50        resource: String,
51    },
52
53    /// Timeout occurred
54    #[cfg_attr(feature = "std", error("Operation timed out after {duration_ms}ms"))]
55    Timeout {
56        /// The duration in milliseconds after which the operation timed out
57        duration_ms: u64,
58    },
59
60    /// Agent capability mismatch
61    #[cfg_attr(
62        feature = "std",
63        error("Agent {agent_id} lacks capability: {capability}")
64    )]
65    CapabilityMismatch {
66        /// The unique identifier of the agent lacking the capability
67        agent_id: String,
68        /// The required capability that the agent lacks
69        capability: String,
70    },
71
72    /// Orchestration strategy error
73    #[cfg_attr(feature = "std", error("Strategy error: {reason}"))]
74    StrategyError {
75        /// Description of the strategy error
76        reason: String,
77    },
78
79    /// Serialization/deserialization error
80    #[cfg_attr(feature = "std", error("Serialization error: {reason}"))]
81    SerializationError {
82        /// Description of the serialization error
83        reason: String,
84    },
85
86    /// Generic error with custom message
87    #[cfg_attr(feature = "std", error("{0}"))]
88    Custom(String),
89}
90
91#[cfg(not(feature = "std"))]
92impl fmt::Display for SwarmError {
93    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
94        match self {
95            SwarmError::AgentNotFound { id } => write!(f, "Agent not found: {}", id),
96            SwarmError::TaskExecutionFailed { reason } => {
97                write!(f, "Task execution failed: {}", reason)
98            }
99            SwarmError::InvalidTopology { reason } => write!(f, "Invalid topology: {}", reason),
100            SwarmError::CommunicationError { reason } => {
101                write!(f, "Communication error: {}", reason)
102            }
103            SwarmError::ResourceExhausted { resource } => {
104                write!(f, "Resource exhausted: {}", resource)
105            }
106            SwarmError::Timeout { duration_ms } => {
107                write!(f, "Operation timed out after {}ms", duration_ms)
108            }
109            SwarmError::CapabilityMismatch {
110                agent_id,
111                capability,
112            } => {
113                write!(f, "Agent {} lacks capability: {}", agent_id, capability)
114            }
115            SwarmError::StrategyError { reason } => write!(f, "Strategy error: {}", reason),
116            SwarmError::SerializationError { reason } => {
117                write!(f, "Serialization error: {}", reason)
118            }
119            SwarmError::Custom(msg) => write!(f, "{}", msg),
120        }
121    }
122}
123
124#[cfg(not(feature = "std"))]
125impl core::error::Error for SwarmError {}
126
127impl SwarmError {
128    /// Create a custom error with a message
129    pub fn custom(msg: impl Into<String>) -> Self {
130        SwarmError::Custom(msg.into())
131    }
132
133    /// Check if the error is retriable
134    pub fn is_retriable(&self) -> bool {
135        matches!(
136            self,
137            SwarmError::CommunicationError { .. }
138                | SwarmError::Timeout { .. }
139                | SwarmError::ResourceExhausted { .. }
140        )
141    }
142}
143
144/// Agent-specific error type
145#[derive(Debug, Clone)]
146pub struct AgentError {
147    /// The unique identifier of the agent that encountered the error
148    pub agent_id: String,
149    /// The specific swarm error that occurred
150    pub error: SwarmError,
151}
152
153impl fmt::Display for AgentError {
154    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
155        write!(f, "Agent {} error: {}", self.agent_id, self.error)
156    }
157}
158
159#[cfg(feature = "std")]
160impl std::error::Error for AgentError {}
161
162#[cfg(not(feature = "std"))]
163impl core::error::Error for AgentError {}