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    /// Security screening failed
35    #[error("Security screening failed: {0}")]
36    ScreeningFailed(String),
37
38    /// Context blocked by security screening
39    #[error("Context blocked: {0}")]
40    Blocked(String),
41
42    /// IO error
43    #[error("IO error: {0}")]
44    Io(#[from] std::io::Error),
45
46    /// Timeout error
47    #[error("Operation timed out: {0}")]
48    Timeout(String),
49
50    /// Configuration error
51    #[error("Configuration error: {0}")]
52    Config(String),
53
54    /// Protocol error
55    #[error("Protocol error: {0}")]
56    Protocol(String),
57
58    /// Internal error
59    #[error("Internal error: {0}")]
60    Internal(String),
61}
62
63impl ContextError {
64    /// Check if this is a not found error
65    pub fn is_not_found(&self) -> bool {
66        matches!(self, Self::NotFound(_))
67    }
68
69    /// Check if this is a security-related error
70    pub fn is_security_error(&self) -> bool {
71        matches!(self, Self::ScreeningFailed(_) | Self::Blocked(_))
72    }
73}
74#[cfg(feature = "persistence")]
75impl From<sled::Error> for ContextError {
76    fn from(err: sled::Error) -> Self {
77        Self::Storage(err.to_string())
78    }
79}