modular_agent_core/
error.rs1use thiserror::Error;
2
3#[derive(Clone, Debug, Error)]
14pub enum AgentError {
15 #[error("Invalid {0} value in array")]
17 InvalidArrayValue(String),
18
19 #[error("{0}: Agent definition \"{1}\" is invalid")]
21 InvalidDefinition(String, String),
22
23 #[error("Invalid port: {0}")]
25 InvalidPin(String),
26
27 #[error("Invalid preset name: {0}")]
29 InvalidPresetName(String),
30
31 #[error("Invalid {0} value")]
33 InvalidValue(String),
34
35 #[error("{0}: Agent definition \"{1}\" is missing")]
37 MissingDefinition(String, String),
38
39 #[error("Failed to rename preset: {0}")]
41 RenamePresetFailed(String),
42
43 #[error("Unknown agent def kind: {0}")]
45 UnknownDefKind(String),
46
47 #[error("Unknown agent def name: {0}")]
49 UnknownDefName(String),
50
51 #[error("Agent definition \"{0}\" is not implemented")]
53 NotImplemented(String),
54
55 #[error("Agent {0} already exists")]
57 AgentAlreadyExists(String),
58
59 #[error("Failed to create agent {0}")]
61 AgentCreationFailed(String),
62
63 #[error("Agent {0} not found")]
65 AgentNotFound(String),
66
67 #[error("Source agent {0} not found")]
69 SourceAgentNotFound(String),
70
71 #[error("Duplicate id: {0}")]
73 DuplicateId(String),
74
75 #[error("Source handle is empty")]
77 EmptySourceHandle,
78
79 #[error("Target handle is empty")]
81 EmptyTargetHandle,
82
83 #[error("Connection already exists")]
85 ConnectionAlreadyExists,
86
87 #[error("Connection {0} not found")]
89 ConnectionNotFound(String),
90
91 #[error("Preset {0} not found")]
93 PresetNotFound(String),
94
95 #[error("Preset name \"{0}\" already exists")]
97 PresetNameExists(String),
98
99 #[error("Agent {0} definition not found")]
101 AgentDefinitionNotFound(String),
102
103 #[error("Agent tx for {0} not found")]
105 AgentTxNotFound(String),
106
107 #[error("Failed to send message: {0}")]
109 SendMessageFailed(String),
110
111 #[error("Failed to serialize/deserialize: {0}")]
113 SerializationError(String),
114
115 #[error("Message sender not initialized")]
117 TxNotInitialized,
118
119 #[error("IO error: {0}")]
121 IoError(String),
122
123 #[error("JSON parsing error: {0}")]
125 JsonParseError(String),
126
127 #[error("Invalid file extension: expected JSON")]
129 InvalidFileExtension,
130
131 #[error("Empty file name")]
133 EmptyFileName,
134
135 #[error("Failed to get file stem from path")]
137 FileSystemError,
138
139 #[error("Configuration error: {0}")]
141 InvalidConfig(String),
142
143 #[error("No configuration available")]
145 NoConfig,
146
147 #[error("Unknown configuration: {0}")]
149 UnknownConfig(String),
150
151 #[error("No global configuration available")]
153 NoGlobalConfig,
154
155 #[error("Pin not found: {0}")]
157 PinNotFound(String),
158
159 #[error("Rate limited: {message}")]
164 RateLimited {
165 message: String,
166 retry_after: Option<std::time::Duration>,
167 },
168
169 #[error("Provider overloaded: {0}")]
171 Overloaded(String),
172
173 #[error("Request timed out: {0}")]
175 Timeout(String),
176
177 #[error("Context overflow: {0}")]
179 ContextOverflow(String),
180
181 #[error("Cancelled")]
183 Cancelled,
184
185 #[error("Agent error: {0}")]
187 Other(String),
188}
189
190impl AgentError {
191 pub fn is_retryable(&self) -> bool {
193 matches!(
194 self,
195 Self::RateLimited { .. } | Self::Overloaded(_) | Self::Timeout(_)
196 )
197 }
198}
199
200#[cfg(test)]
201mod tests {
202 use super::*;
203
204 #[test]
205 fn retryable_variants_are_retryable() {
206 assert!(
207 AgentError::RateLimited {
208 message: "slow down".into(),
209 retry_after: None,
210 }
211 .is_retryable()
212 );
213 assert!(AgentError::Overloaded("busy".into()).is_retryable());
214 assert!(AgentError::Timeout("deadline exceeded".into()).is_retryable());
215 }
216
217 #[test]
218 fn non_retryable_variants_are_not_retryable() {
219 assert!(!AgentError::ContextOverflow("too long".into()).is_retryable());
220 assert!(!AgentError::Cancelled.is_retryable());
221 assert!(!AgentError::InvalidValue("bad".into()).is_retryable());
222 assert!(!AgentError::IoError("disk full".into()).is_retryable());
223 }
224}