Skip to main content

ggen_core/domain/
error.rs

1//! Domain-specific error types for ggen
2//!
3//! This module defines error types specific to the domain layer,
4//! including A2A-RS integration errors and other domain-specific failures.
5
6use crate::utils::error::Error;
7use thiserror::Error;
8
9/// A2A-RS integration errors
10#[derive(Error, Debug)]
11pub enum A2aError {
12    #[error("A2A connection failed: {0}")]
13    Connection(String),
14
15    #[error("A2A authentication failed: {0}")]
16    Authentication(String),
17
18    #[error("A2A agent not found: {0}")]
19    AgentNotFound(String),
20
21    #[error("A2A agent already running: {0}")]
22    AgentAlreadyRunning(String),
23
24    #[error("A2A invalid configuration: {0}")]
25    InvalidConfiguration(String),
26
27    #[error("A2A timeout: {0}")]
28    Timeout(String),
29
30    #[error("A2A protocol error: {0}")]
31    Protocol(String),
32
33    #[error("A2A transport error: {0}")]
34    Transport(String),
35
36    #[error("A2A message delivery failed: {0}")]
37    MessageDelivery(String),
38
39    #[error("A2A resource not found: {0}")]
40    ResourceNotFound(String),
41
42    #[error("A2A permission denied: {0}")]
43    PermissionDenied(String),
44
45    #[error("A2A internal error: {0}")]
46    Internal(String),
47}
48
49/// Convert A2A errors to domain errors
50impl From<A2aError> for Error {
51    fn from(err: A2aError) -> Self {
52        match err {
53            A2aError::Connection(msg) => Error::network_error(msg),
54            A2aError::Authentication(msg) => Error::invalid_input(msg),
55            A2aError::AgentNotFound(msg) => Error::file_not_found(std::path::PathBuf::from(msg)),
56            A2aError::AgentAlreadyRunning(msg) => Error::invalid_state(msg),
57            A2aError::InvalidConfiguration(msg) => Error::invalid_input(msg),
58            A2aError::Timeout(msg) => Error::new(&format!("Timeout: {}", msg)),
59            A2aError::Protocol(msg) => Error::new(&format!("Protocol error: {}", msg)),
60            A2aError::Transport(msg) => Error::network_error(msg),
61            A2aError::MessageDelivery(msg) => {
62                Error::new(&format!("Message delivery failed: {}", msg))
63            }
64            A2aError::ResourceNotFound(msg) => Error::file_not_found(std::path::PathBuf::from(msg)),
65            A2aError::PermissionDenied(msg) => Error::invalid_input(msg),
66            A2aError::Internal(msg) => Error::internal_error(msg),
67        }
68    }
69}
70
71/// MCP integration errors
72#[derive(Error, Debug)]
73pub enum McpError {
74    #[error("MCP server connection failed: {0}")]
75    ServerConnection(String),
76
77    #[error("MCP server not found: {0}")]
78    ServerNotFound(String),
79
80    #[error("MCP tool not found: {0}")]
81    ToolNotFound(String),
82
83    #[error("MCP request failed: {0}")]
84    RequestFailed(String),
85
86    #[error("MCP response parsing failed: {0}")]
87    ResponseParse(String),
88
89    #[error("MCP authentication failed: {0}")]
90    Authentication(String),
91
92    #[error("MCP configuration error: {0}")]
93    Configuration(String),
94
95    #[error("MCP timeout: {0}")]
96    Timeout(String),
97
98    #[error("MCP internal error: {0}")]
99    Internal(String),
100}
101
102/// Convert MCP errors to domain errors
103impl From<McpError> for Error {
104    fn from(err: McpError) -> Self {
105        match err {
106            McpError::ServerConnection(msg) => Error::network_error(msg),
107            McpError::ServerNotFound(msg) => Error::file_not_found(std::path::PathBuf::from(msg)),
108            McpError::ToolNotFound(msg) => Error::file_not_found(std::path::PathBuf::from(msg)),
109            McpError::RequestFailed(msg) => Error::new(&format!("Request failed: {}", msg)),
110            McpError::ResponseParse(msg) => {
111                Error::new(&format!("Response parsing failed: {}", msg))
112            }
113            McpError::Authentication(msg) => Error::invalid_input(msg),
114            McpError::Configuration(msg) => Error::invalid_input(msg),
115            McpError::Timeout(msg) => Error::new(&format!("Timeout: {}", msg)),
116            McpError::Internal(msg) => Error::internal_error(msg),
117        }
118    }
119}
120
121/// Agent lifecycle errors
122#[derive(Error, Debug)]
123pub enum AgentError {
124    #[error("Agent startup failed: {0}")]
125    StartupFailed(String),
126
127    #[error("Agent shutdown failed: {0}")]
128    ShutdownFailed(String),
129
130    #[error("Agent communication failed: {0}")]
131    CommunicationFailed(String),
132
133    #[error("Agent state transition failed: {0}")]
134    StateTransitionFailed(String),
135
136    #[error("Agent configuration invalid: {0}")]
137    InvalidConfiguration(String),
138
139    #[error("Agent capability not supported: {0}")]
140    CapabilityNotSupported(String),
141
142    #[error("Agent timeout: {0}")]
143    Timeout(String),
144
145    #[error("Agent internal error: {0}")]
146    Internal(String),
147}
148
149/// Convert agent errors to domain errors
150impl From<AgentError> for Error {
151    fn from(err: AgentError) -> Self {
152        match err {
153            AgentError::StartupFailed(msg) => Error::invalid_state(msg),
154            AgentError::ShutdownFailed(msg) => Error::invalid_state(msg),
155            AgentError::CommunicationFailed(msg) => Error::network_error(msg),
156            AgentError::StateTransitionFailed(msg) => Error::invalid_state(msg),
157            AgentError::InvalidConfiguration(msg) => Error::invalid_input(msg),
158            AgentError::CapabilityNotSupported(msg) => Error::feature_not_enabled(&msg, ""),
159            AgentError::Timeout(msg) => Error::new(&format!("Timeout: {}", msg)),
160            AgentError::Internal(msg) => Error::internal_error(msg),
161        }
162    }
163}
164
165/// Helper function for creating A2A connection errors
166pub fn a2a_connection_error<S: Into<String>>(message: S) -> A2aError {
167    A2aError::Connection(message.into())
168}
169
170/// Helper function for creating A2A authentication errors
171pub fn a2a_auth_error<S: Into<String>>(message: S) -> A2aError {
172    A2aError::Authentication(message.into())
173}
174
175/// Helper function for creating MCP server errors
176pub fn mcp_server_error<S: Into<String>>(message: S) -> McpError {
177    McpError::ServerConnection(message.into())
178}
179
180/// Helper function for creating agent startup errors
181pub fn agent_startup_error<S: Into<String>>(message: S) -> AgentError {
182    AgentError::StartupFailed(message.into())
183}
184
185#[cfg(test)]
186mod tests {
187    use super::*;
188
189    #[test]
190    fn test_a2a_error_conversion() {
191        let a2a_error = A2aError::Connection("Connection failed".to_string());
192        let domain_error: Error = a2a_error.into();
193        assert!(domain_error.to_string().contains("Connection failed"));
194    }
195
196    #[test]
197    fn test_mcp_error_conversion() {
198        let mcp_error = McpError::ToolNotFound("tool not found".to_string());
199        let domain_error: Error = mcp_error.into();
200        assert!(domain_error.to_string().contains("tool not found"));
201    }
202
203    #[test]
204    fn test_agent_error_conversion() {
205        let agent_error = AgentError::StartupFailed("startup failed".to_string());
206        let domain_error: Error = agent_error.into();
207        assert!(domain_error.to_string().contains("startup failed"));
208    }
209
210    #[test]
211    fn test_a2a_helper_functions() {
212        let conn_error = a2a_connection_error("test connection error");
213        assert!(matches!(conn_error, A2aError::Connection(_)));
214
215        let auth_error = a2a_auth_error("test auth error");
216        assert!(matches!(auth_error, A2aError::Authentication(_)));
217    }
218
219    #[test]
220    fn test_mcp_helper_functions() {
221        let server_error = mcp_server_error("test server error");
222        assert!(matches!(server_error, McpError::ServerConnection(_)));
223    }
224
225    #[test]
226    fn test_agent_helper_functions() {
227        let startup_error = agent_startup_error("test startup error");
228        assert!(matches!(startup_error, AgentError::StartupFailed(_)));
229    }
230}