Skip to main content

aagt_core/
error.rs

1use anyhow::Error as AnyhowError;
2use thiserror::Error;
3
4/// Result type alias using aagt's Error
5pub type Result<T> = std::result::Result<T, Error>;
6
7/// Main error type for the aagt framework
8#[derive(Debug, Error)]
9pub enum Error {
10    // ... (variants) ...
11    // ============ Agent Errors ============
12    /// Agent is not properly configured
13    #[error("Agent configuration error: {0}")]
14    AgentConfig(String),
15
16    /// Agent execution failed
17    #[error("Agent execution error: {0}")]
18    AgentExecution(String),
19
20    // ============ Provider Errors ============
21    /// Provider API error
22    #[error("Provider API error: {0}")]
23    ProviderApi(String),
24
25    /// Provider authentication failed
26    #[error("Provider authentication error: {0}")]
27    ProviderAuth(String),
28
29    /// Provider rate limit exceeded
30    #[error("Provider rate limit exceeded: retry after {retry_after_secs}s")]
31    ProviderRateLimit {
32        /// Seconds to wait before retrying
33        retry_after_secs: u64,
34    },
35
36    // ============ Tool Errors ============
37    /// Tool not found in agent's toolset
38    #[error("Tool not found: {0}")]
39    ToolNotFound(String),
40
41    /// Tool execution failed
42    #[error("Tool execution error: {tool_name} - {message}")]
43    ToolExecution {
44        /// Name of the tool that failed
45        tool_name: String,
46        /// Error message
47        message: String,
48    },
49
50    /// Tool approval required
51    #[error("Tool execution blocked: {tool_name} requires approval but no handler was available")]
52    ToolApprovalRequired {
53        /// Name of the tool
54        tool_name: String,
55    },
56
57    /// Invalid tool arguments
58    #[error("Invalid tool arguments for {tool_name}: {message}")]
59    ToolArguments {
60        /// Name of the tool
61        tool_name: String,
62        /// Error message
63        message: String,
64    },
65
66    // ============ Message Errors ============
67    /// Message parsing failed
68    #[error("Message parse error: {0}")]
69    MessageParse(String),
70
71    /// Message serialization failed
72    #[error("Message serialization error: {0}")]
73    MessageSerialize(#[from] serde_json::Error),
74
75    // ============ Streaming Errors ============
76    /// Stream interrupted
77    #[error("Stream interrupted: {0}")]
78    StreamInterrupted(String),
79
80    /// Stream timeout
81    #[error("Stream timeout after {timeout_secs}s")]
82    StreamTimeout {
83        /// Timeout duration in seconds
84        timeout_secs: u64,
85    },
86
87    // ============ Memory Errors ============
88    /// Memory storage error
89    #[error("Memory storage error: {0}")]
90    MemoryStorage(String),
91
92    /// Memory retrieval error
93    #[error("Memory retrieval error: {0}")]
94    MemoryRetrieval(String),
95
96    // ============ Strategy Errors ============
97    /// Strategy configuration error
98    #[cfg(feature = "trading")]
99    #[error("Strategy configuration error: {0}")]
100    StrategyConfig(String),
101
102    /// Strategy execution error
103    #[cfg(feature = "trading")]
104    #[error("Strategy execution error: {0}")]
105    StrategyExecution(String),
106
107    /// Condition evaluation error
108    #[cfg(feature = "trading")]
109    #[error("Condition evaluation error: {0}")]
110    ConditionEvaluation(String),
111
112    // ============ Risk Control Errors ============
113    /// Risk check failed - transaction blocked
114    #[cfg(feature = "trading")]
115    #[error("Risk check failed: {check_name} - {reason}")]
116    RiskCheckFailed {
117        /// Name of the risk check
118        check_name: String,
119        /// Reason for failure
120        reason: String,
121    },
122
123    /// Risk limit exceeded
124    #[cfg(feature = "trading")]
125    #[error("Risk limit exceeded: {limit_type} - current: {current}, max: {max}")]
126    RiskLimitExceeded {
127        /// Type of limit
128        limit_type: String,
129        /// Current value
130        current: String,
131        /// Maximum allowed value
132        max: String,
133    },
134
135    // ============ Simulation Errors ============
136    /// Simulation failed
137    #[cfg(feature = "trading")]
138    #[error("Simulation error: {0}")]
139    Simulation(String),
140
141    // ============ Multi-Agent Errors ============
142    /// Agent coordination error
143    #[error("Agent coordination error: {0}")]
144    AgentCoordination(String),
145
146    /// Agent communication error
147    #[error("Agent communication error: {0}")]
148    AgentCommunication(String),
149
150    // ============ Network Errors ============
151    /// HTTP request failed
152    #[error("HTTP error: {0}")]
153    Http(#[from] reqwest::Error),
154
155    // ============ System Errors ============
156    /// IO error
157    #[error("IO error: {0}")]
158    Io(#[from] std::io::Error),
159
160    // ============ Generic Errors ============
161    /// Internal error
162    #[error("Internal error: {0}")]
163    Internal(String),
164
165    /// Any other error
166    #[error("{0}")]
167    Other(AnyhowError),
168}
169
170impl Error {
171    /// Create a new agent configuration error
172    pub fn agent_config(msg: impl Into<String>) -> Self {
173        Self::AgentConfig(msg.into())
174    }
175
176    /// Create a new tool execution error
177    pub fn tool_execution(tool_name: impl Into<String>, message: impl Into<String>) -> Self {
178        Self::ToolExecution {
179            tool_name: tool_name.into(),
180            message: message.into(),
181        }
182    }
183
184    /// Create a new risk check failed error
185    #[cfg(feature = "trading")]
186    pub fn risk_check_failed(check_name: impl Into<String>, reason: impl Into<String>) -> Self {
187        Self::RiskCheckFailed {
188            check_name: check_name.into(),
189            reason: reason.into(),
190        }
191    }
192
193    /// Check if this error is retryable
194    pub fn is_retryable(&self) -> bool {
195        matches!(
196            self,
197            Self::ProviderRateLimit { .. }
198                | Self::StreamInterrupted(_)
199                | Self::StreamTimeout { .. }
200                | Self::Http(_)
201        )
202    }
203}