Skip to main content

mcpmem_core/
errors.rs

1use thiserror::Error;
2
3#[derive(Error, Debug)]
4pub enum MCSError {
5    #[error("Parse error: {0}")]
6    ParseError(String),
7
8    #[error("Method not found: {0}")]
9    MethodNotFound(String),
10
11    #[error("Invalid params: {0}")]
12    InvalidParams(String),
13
14    #[error("Insufficient scope: {tool} needs {scope}")]
15    InsufficientScope { tool: String, scope: &'static str },
16
17    #[error("Memory error: {0}")]
18    MemoryError(String),
19
20    /// A SQLite `UNIQUE` constraint refused a write the caller could have
21    /// avoided — the duplicate-key case of the admin API. Named as its own
22    /// variant so a handler can answer an avoidable conflict (409) instead
23    /// of a store fault (500).
24    #[error("Constraint violation: {0}")]
25    ConstraintViolation(String),
26
27    #[error("IO error: {0}")]
28    IoError(#[from] std::io::Error),
29
30    #[error("JSON error: {0}")]
31    JsonError(#[from] serde_json::Error),
32
33    #[error("Serialization error: {0}")]
34    SerializationError(String),
35}
36
37impl MCSError {
38    pub const fn error_code(&self) -> i64 {
39        match self {
40            MCSError::ParseError(_) => -32700,
41            MCSError::MethodNotFound(_) => -32601,
42            MCSError::InvalidParams(_) => -32602,
43            MCSError::InsufficientScope { .. } => -32002,
44            MCSError::MemoryError(_) => -32000,
45            MCSError::ConstraintViolation(_) => -32005,
46            MCSError::IoError(_) => -32003,
47            MCSError::JsonError(_) => -32700,
48            MCSError::SerializationError(_) => -32004,
49        }
50    }
51}
52
53pub type Result<T> = std::result::Result<T, MCSError>;