cnctd-service-ssh 0.1.8

SSH command execution service - library and MCP server
Documentation
//! Error types for the SSH service.
//!
//! Provides structured errors that can be converted to MCP errors when needed.

use std::fmt;

/// Errors returned from SSH service operations
#[derive(Debug)]
pub enum ServiceError {
    /// Invalid parameters provided
    InvalidParams(String),
    /// Resource not found (e.g., unknown target ID)
    NotFound(String),
    /// Internal error (lock poisoned, IO error, etc.)
    Internal(String),
    /// JSON serialization/deserialization error
    SerdeError(serde_json::Error),
}

impl fmt::Display for ServiceError {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            ServiceError::InvalidParams(msg) => write!(f, "Invalid parameters: {}", msg),
            ServiceError::NotFound(msg) => write!(f, "Not found: {}", msg),
            ServiceError::Internal(msg) => write!(f, "Internal error: {}", msg),
            ServiceError::SerdeError(e) => write!(f, "Serialization error: {}", e),
        }
    }
}

impl std::error::Error for ServiceError {}

impl From<serde_json::Error> for ServiceError {
    fn from(err: serde_json::Error) -> Self {
        ServiceError::SerdeError(err)
    }
}

// MCP error conversion - only available with mcp feature
#[cfg(feature = "mcp")]
impl From<ServiceError> for rmcp::model::ErrorData {
    fn from(err: ServiceError) -> Self {
        match err {
            ServiceError::InvalidParams(msg) => {
                rmcp::model::ErrorData::invalid_params(msg, None)
            }
            ServiceError::NotFound(msg) => {
                rmcp::model::ErrorData::resource_not_found(msg, None)
            }
            ServiceError::Internal(msg) => {
                rmcp::model::ErrorData::internal_error(msg, None)
            }
            ServiceError::SerdeError(e) => {
                rmcp::model::ErrorData::internal_error(format!("JSON error: {}", e), None)
            }
        }
    }
}