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    #[error("Strategy configuration error: {0}")]
99    StrategyConfig(String),
100
101    /// Strategy execution error
102    #[error("Strategy execution error: {0}")]
103    StrategyExecution(String),
104
105    /// Condition evaluation error
106    #[error("Condition evaluation error: {0}")]
107    ConditionEvaluation(String),
108
109    // ============ Risk Control Errors ============
110    /// Risk check failed - transaction blocked
111    #[error("Risk check failed: {check_name} - {reason}")]
112    RiskCheckFailed {
113        /// Name of the risk check
114        check_name: String,
115        /// Reason for failure
116        reason: String,
117    },
118
119    /// Risk limit exceeded
120    #[error("Risk limit exceeded: {limit_type} - current: {current}, max: {max}")]
121    RiskLimitExceeded {
122        /// Type of limit
123        limit_type: String,
124        /// Current value
125        current: String,
126        /// Maximum allowed value
127        max: String,
128    },
129
130    // ============ Simulation Errors ============
131    /// Simulation failed
132    #[error("Simulation error: {0}")]
133    Simulation(String),
134
135    // ============ Multi-Agent Errors ============
136    /// Agent coordination error
137    #[error("Agent coordination error: {0}")]
138    AgentCoordination(String),
139
140    /// Agent communication error
141    #[error("Agent communication error: {0}")]
142    AgentCommunication(String),
143
144    // ============ Network Errors ============
145    /// HTTP request failed
146    #[error("HTTP error: {0}")]
147    Http(#[from] reqwest::Error),
148
149    // ============ System Errors ============
150    /// IO error
151    #[error("IO error: {0}")]
152    Io(#[from] std::io::Error),
153
154    // ============ Generic Errors ============
155    /// Internal error
156    #[error("Internal error: {0}")]
157    Internal(String),
158
159    /// Any other error
160    #[error("{0}")]
161    Other(AnyhowError),
162}
163
164impl Error {
165    /// Create a new agent configuration error
166    pub fn agent_config(msg: impl Into<String>) -> Self {
167        Self::AgentConfig(msg.into())
168    }
169
170    /// Create a new tool execution error
171    pub fn tool_execution(tool_name: impl Into<String>, message: impl Into<String>) -> Self {
172        Self::ToolExecution {
173            tool_name: tool_name.into(),
174            message: message.into(),
175        }
176    }
177
178    /// Create a new risk check failed error
179    pub fn risk_check_failed(check_name: impl Into<String>, reason: impl Into<String>) -> Self {
180        Self::RiskCheckFailed {
181            check_name: check_name.into(),
182            reason: reason.into(),
183        }
184    }
185
186    /// Check if this error is retryable
187    pub fn is_retryable(&self) -> bool {
188        matches!(
189            self,
190            Self::ProviderRateLimit { .. }
191                | Self::StreamInterrupted(_)
192                | Self::StreamTimeout { .. }
193                | Self::Http(_)
194        )
195    }
196}