use thiserror::Error;
#[derive(Debug, Error)]
pub enum SshMcpError {
#[error("SSH connection error: {0}")]
Connection(String),
#[error("Authentication failed: {0}")]
Authentication(String),
#[error("Command timeout after {0}ms")]
Timeout(u64),
#[error("Invalid parameters: {0}")]
InvalidParams(String),
#[error("Elevation failed: {0}")]
ElevationFailed(String),
#[error("Configuration error: {0}")]
Config(String),
#[error("IO error: {0}")]
Io(#[from] std::io::Error),
#[error("SSH key error: {0}")]
SshKey(String),
}
pub type Result<T> = std::result::Result<T, SshMcpError>;
impl SshMcpError {
pub fn connection(msg: impl Into<String>) -> Self {
SshMcpError::Connection(msg.into())
}
pub fn auth(msg: impl Into<String>) -> Self {
SshMcpError::Authentication(msg.into())
}
pub fn invalid_params(msg: impl Into<String>) -> Self {
SshMcpError::InvalidParams(msg.into())
}
pub fn elevation_failed(msg: impl Into<String>) -> Self {
SshMcpError::ElevationFailed(msg.into())
}
pub fn config(msg: impl Into<String>) -> Self {
SshMcpError::Config(msg.into())
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_error_display() {
let err = SshMcpError::Connection("failed to connect".to_string());
assert_eq!(err.to_string(), "SSH connection error: failed to connect");
let err = SshMcpError::Timeout(5000);
assert_eq!(err.to_string(), "Command timeout after 5000ms");
}
}