vtcode_acp_client/
error.rs

1//! Error types for ACP operations
2
3use std::fmt;
4
5/// Result type for ACP operations
6pub type AcpResult<T> = std::result::Result<T, AcpError>;
7
8/// Errors that can occur during ACP communication
9#[derive(Debug)]
10pub enum AcpError {
11    /// Agent not found or unavailable
12    AgentNotFound(String),
13
14    /// Network/HTTP error
15    NetworkError(String),
16
17    /// Message serialization/deserialization error
18    SerializationError(String),
19
20    /// Invalid request format
21    InvalidRequest(String),
22
23    /// Remote agent returned an error
24    RemoteError {
25        agent_id: String,
26        message: String,
27        code: Option<i32>,
28    },
29
30    /// Request timeout
31    Timeout(String),
32
33    /// Configuration error
34    ConfigError(String),
35
36    /// Generic internal error
37    Internal(String),
38}
39
40impl fmt::Display for AcpError {
41    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
42        match self {
43            AcpError::AgentNotFound(id) => write!(f, "Agent not found: {}", id),
44            AcpError::NetworkError(e) => write!(f, "Network error: {}", e),
45            AcpError::SerializationError(e) => write!(f, "Serialization error: {}", e),
46            AcpError::InvalidRequest(e) => write!(f, "Invalid request: {}", e),
47            AcpError::RemoteError {
48                agent_id,
49                message,
50                code,
51            } => {
52                write!(f, "Remote error from {}: {}", agent_id, message)?;
53                if let Some(code) = code {
54                    write!(f, " (code: {})", code)?;
55                }
56                Ok(())
57            }
58            AcpError::Timeout(e) => write!(f, "Timeout: {}", e),
59            AcpError::ConfigError(e) => write!(f, "Configuration error: {}", e),
60            AcpError::Internal(e) => write!(f, "Internal error: {}", e),
61        }
62    }
63}
64
65impl std::error::Error for AcpError {}
66
67impl From<reqwest::Error> for AcpError {
68    fn from(err: reqwest::Error) -> Self {
69        AcpError::NetworkError(err.to_string())
70    }
71}
72
73impl From<serde_json::Error> for AcpError {
74    fn from(err: serde_json::Error) -> Self {
75        AcpError::SerializationError(err.to_string())
76    }
77}
78
79impl From<anyhow::Error> for AcpError {
80    fn from(err: anyhow::Error) -> Self {
81        AcpError::Internal(err.to_string())
82    }
83}