Skip to main content

cnctd_service_ssh/
service_error.rs

1//! Error types for the SSH service.
2//!
3//! Provides structured errors that can be converted to MCP errors when needed.
4
5use std::fmt;
6
7/// Errors returned from SSH service operations
8#[derive(Debug)]
9pub enum ServiceError {
10    /// Invalid parameters provided
11    InvalidParams(String),
12    /// Resource not found (e.g., unknown target ID)
13    NotFound(String),
14    /// Internal error (lock poisoned, IO error, etc.)
15    Internal(String),
16    /// JSON serialization/deserialization error
17    SerdeError(serde_json::Error),
18}
19
20impl fmt::Display for ServiceError {
21    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
22        match self {
23            ServiceError::InvalidParams(msg) => write!(f, "Invalid parameters: {}", msg),
24            ServiceError::NotFound(msg) => write!(f, "Not found: {}", msg),
25            ServiceError::Internal(msg) => write!(f, "Internal error: {}", msg),
26            ServiceError::SerdeError(e) => write!(f, "Serialization error: {}", e),
27        }
28    }
29}
30
31impl std::error::Error for ServiceError {}
32
33impl From<serde_json::Error> for ServiceError {
34    fn from(err: serde_json::Error) -> Self {
35        ServiceError::SerdeError(err)
36    }
37}
38
39// MCP error conversion - only available with mcp feature
40#[cfg(feature = "mcp")]
41impl From<ServiceError> for rmcp::model::ErrorData {
42    fn from(err: ServiceError) -> Self {
43        match err {
44            ServiceError::InvalidParams(msg) => {
45                rmcp::model::ErrorData::invalid_params(msg, None)
46            }
47            ServiceError::NotFound(msg) => {
48                rmcp::model::ErrorData::resource_not_found(msg, None)
49            }
50            ServiceError::Internal(msg) => {
51                rmcp::model::ErrorData::internal_error(msg, None)
52            }
53            ServiceError::SerdeError(e) => {
54                rmcp::model::ErrorData::internal_error(format!("JSON error: {}", e), None)
55            }
56        }
57    }
58}