Skip to main content

ssh_mcp/
error.rs

1//! Error types for SeSSHion
2
3use thiserror::Error;
4
5/// Main error type for SeSSHion
6#[derive(Debug, Error)]
7pub enum SshMcpError {
8    /// SSH connection failed
9    #[error("SSH connection error: {0}")]
10    Connection(String),
11
12    /// Authentication failed (password, key, or su/sudo)
13    #[error("Authentication failed: {0}")]
14    Authentication(String),
15
16    /// Command execution timed out
17    #[error("Command timeout after {0}ms")]
18    Timeout(u64),
19
20    /// Invalid parameters provided
21    #[error("Invalid parameters: {0}")]
22    InvalidParams(String),
23
24    /// su/sudo elevation failed
25    #[error("Elevation failed: {0}")]
26    ElevationFailed(String),
27
28    /// Configuration error
29    #[error("Configuration error: {0}")]
30    Config(String),
31
32    /// IO error
33    #[error("IO error: {0}")]
34    Io(#[from] std::io::Error),
35
36    /// SSH key parsing error
37    #[error("SSH key error: {0}")]
38    SshKey(String),
39}
40
41/// Result type alias using SshMcpError
42pub type Result<T> = std::result::Result<T, SshMcpError>;
43
44impl SshMcpError {
45    /// Create a connection error from a string
46    pub fn connection(msg: impl Into<String>) -> Self {
47        SshMcpError::Connection(msg.into())
48    }
49
50    /// Create an authentication error from a string
51    pub fn auth(msg: impl Into<String>) -> Self {
52        SshMcpError::Authentication(msg.into())
53    }
54
55    /// Create an invalid params error from a string
56    pub fn invalid_params(msg: impl Into<String>) -> Self {
57        SshMcpError::InvalidParams(msg.into())
58    }
59
60    /// Create an elevation failed error from a string
61    pub fn elevation_failed(msg: impl Into<String>) -> Self {
62        SshMcpError::ElevationFailed(msg.into())
63    }
64
65    /// Create a config error from a string
66    pub fn config(msg: impl Into<String>) -> Self {
67        SshMcpError::Config(msg.into())
68    }
69}
70
71#[cfg(test)]
72mod tests {
73    use super::*;
74
75    #[test]
76    fn test_error_display() {
77        let err = SshMcpError::Connection("failed to connect".to_string());
78        assert_eq!(err.to_string(), "SSH connection error: failed to connect");
79
80        let err = SshMcpError::Timeout(5000);
81        assert_eq!(err.to_string(), "Command timeout after 5000ms");
82    }
83}