1use core::fmt;
4
5#[cfg(not(feature = "std"))]
6use alloc::string::String;
7
8#[cfg(feature = "std")]
9use thiserror::Error;
10
11pub type Result<T> = core::result::Result<T, SwarmError>;
13
14#[derive(Debug, Clone, PartialEq, Eq)]
16#[cfg_attr(feature = "std", derive(Error))]
17pub enum SwarmError {
18 #[cfg_attr(feature = "std", error("Agent not found: {id}"))]
20 AgentNotFound {
21 id: String,
23 },
24
25 #[cfg_attr(feature = "std", error("Task execution failed: {reason}"))]
27 TaskExecutionFailed {
28 reason: String,
30 },
31
32 #[cfg_attr(feature = "std", error("Invalid topology: {reason}"))]
34 InvalidTopology {
35 reason: String,
37 },
38
39 #[cfg_attr(feature = "std", error("Communication error: {reason}"))]
41 CommunicationError {
42 reason: String,
44 },
45
46 #[cfg_attr(feature = "std", error("Resource exhausted: {resource}"))]
48 ResourceExhausted {
49 resource: String,
51 },
52
53 #[cfg_attr(feature = "std", error("Operation timed out after {duration_ms}ms"))]
55 Timeout {
56 duration_ms: u64,
58 },
59
60 #[cfg_attr(
62 feature = "std",
63 error("Agent {agent_id} lacks capability: {capability}")
64 )]
65 CapabilityMismatch {
66 agent_id: String,
68 capability: String,
70 },
71
72 #[cfg_attr(feature = "std", error("Strategy error: {reason}"))]
74 StrategyError {
75 reason: String,
77 },
78
79 #[cfg_attr(feature = "std", error("Serialization error: {reason}"))]
81 SerializationError {
82 reason: String,
84 },
85
86 #[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 pub fn custom(msg: impl Into<String>) -> Self {
130 SwarmError::Custom(msg.into())
131 }
132
133 pub fn is_retriable(&self) -> bool {
135 matches!(
136 self,
137 SwarmError::CommunicationError { .. }
138 | SwarmError::Timeout { .. }
139 | SwarmError::ResourceExhausted { .. }
140 )
141 }
142}
143
144#[derive(Debug, Clone)]
146pub struct AgentError {
147 pub agent_id: String,
149 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 {}