context_mcp/
error.rs

1//! Error types for the context MCP server
2
3use thiserror::Error;
4
5/// Result type alias for context operations
6pub type Result<T> = std::result::Result<T, ContextError>;
7
8/// Result type alias (alternative name)
9pub type ContextResult<T> = std::result::Result<T, ContextError>;
10
11/// Errors that can occur in context operations
12#[derive(Error, Debug)]
13pub enum ContextError {
14    /// Context not found
15    #[error("Context not found: {0}")]
16    NotFound(String),
17
18    /// Storage error
19    #[error("Storage error: {0}")]
20    Storage(String),
21
22    /// Serialization error
23    #[error("Serialization error: {0}")]
24    Serialization(#[from] serde_json::Error),
25
26    /// Invalid query
27    #[error("Invalid query: {0}")]
28    InvalidQuery(String),
29
30    /// Context expired
31    #[error("Context has expired: {0}")]
32    Expired(String),
33
34
35    /// Security screening failed
36    #[error("Security screening failed: {0}")]
37    ScreeningFailed(String),
38
39    /// Context blocked by security screening
40    #[error("Context blocked: {0}")]
41    Blocked(String),
42
43    /// IO error
44    #[error("IO error: {0}")]
45    Io(#[from] std::io::Error),
46
47    /// Timeout error
48    #[error("Operation timed out: {0}")]
49    Timeout(String),
50
51    /// Configuration error
52    #[error("Configuration error: {0}")]
53    Config(String),
54
55    /// Protocol error
56    #[error("Protocol error: {0}")]
57    Protocol(String),
58
59    /// Internal error
60    #[error("Internal error: {0}")]
61    Internal(String),
62}
63
64impl ContextError {
65    /// Check if this is a not found error
66    pub fn is_not_found(&self) -> bool {
67        matches!(self, Self::NotFound(_))
68    }
69
70    /// Check if this is a security-related error
71    pub fn is_security_error(&self) -> bool {
72        matches!(self, Self::ScreeningFailed(_) | Self::Blocked(_))
73    }
74}
75#[cfg(feature = "persistence")]impl From<sled::Error> for ContextError {
76    fn from(err: sled::Error) -> Self {
77        Self::Storage(err.to_string())
78    }
79}