Skip to main content

memory_mcp/
error.rs

1use thiserror::Error;
2
3/// Errors produced by the memory engine.
4#[derive(Debug, Error)]
5pub enum MemoryError {
6    /// An operation on the git-backed store failed.
7    #[error("git error: {0}")]
8    Git(#[from] git2::Error),
9
10    /// The embedding backend failed to produce vectors.
11    #[error("embedding error: {0}")]
12    Embedding(String),
13
14    /// The vector index could not complete the requested operation.
15    #[error("index error: {0}")]
16    Index(String),
17
18    /// A filesystem I/O error occurred.
19    #[error("io error: {0}")]
20    Io(#[from] std::io::Error),
21
22    /// The requested memory does not exist.
23    #[error("memory not found: {name}")]
24    NotFound {
25        /// Name of the missing memory.
26        name: String,
27    },
28
29    /// The caller provided invalid parameters.
30    #[error("invalid input: {reason}")]
31    InvalidInput {
32        /// Why the input was rejected.
33        reason: String,
34    },
35
36    /// Authentication failed (e.g. bad credentials).
37    #[error("auth error: {0}")]
38    Auth(String),
39
40    /// An OAuth flow error occurred.
41    #[error("oauth error: {0}")]
42    OAuth(String),
43
44    /// The credential store could not read or write a token.
45    #[error("token storage error: {0}")]
46    TokenStorage(String),
47
48    /// YAML serialisation or deserialisation failed.
49    #[error("yaml error: {0}")]
50    Yaml(#[from] serde_yaml::Error),
51
52    /// A background task failed to join.
53    #[error("task join error: {0}")]
54    Join(String),
55
56    /// Catch-all for unexpected internal failures.
57    #[error("internal error: {0}")]
58    Internal(String),
59}
60
61impl From<MemoryError> for rmcp::model::ErrorData {
62    fn from(err: MemoryError) -> Self {
63        let code = match &err {
64            MemoryError::NotFound { .. } | MemoryError::InvalidInput { .. } => {
65                rmcp::model::ErrorCode::INVALID_PARAMS
66            }
67            _ => rmcp::model::ErrorCode::INTERNAL_ERROR,
68        };
69        rmcp::model::ErrorData {
70            code,
71            message: std::borrow::Cow::Owned(err.to_string()),
72            data: None,
73        }
74    }
75}