skp_cache_core/
error.rs

1//! Error types for cache operations
2
3use thiserror::Error;
4
5/// Main error type for all cache operations
6#[derive(Error, Debug, Clone)]
7pub enum CacheError {
8    /// Key not found in cache
9    #[error("key not found: {0}")]
10    NotFound(String),
11
12    /// Serialization failed
13    #[error("serialization error: {0}")]
14    Serialization(String),
15
16    /// Deserialization failed
17    #[error("deserialization error: {0}")]
18    Deserialization(String),
19
20    /// Backend connection failed
21    #[error("connection error: {0}")]
22    Connection(String),
23
24    /// Backend operation failed
25    #[error("backend error: {0}")]
26    Backend(String),
27
28    /// Cyclic dependency detected
29    #[error("cyclic dependency detected for key: {0}")]
30    CyclicDependency(String),
31
32    /// Lock acquisition failed
33    #[error("lock conflict for key: {0}")]
34    LockConflict(String),
35
36    /// Version mismatch for conditional operation
37    #[error("version mismatch: expected {expected}, got {actual}")]
38    VersionMismatch { expected: u64, actual: u64 },
39
40    /// Capacity exceeded
41    #[error("capacity exceeded")]
42    CapacityExceeded,
43
44    /// Internal error
45    #[error("internal error: {0}")]
46    Internal(String),
47
48    /// Compression failed
49    #[error("compression error: {0}")]
50    Compression(String),
51
52    /// Decompression failed
53    #[error("decompression error: {0}")]
54    Decompression(String),
55
56    /// Timeout
57    #[error("operation timed out")]
58    Timeout,
59}
60
61/// Result type alias for cache operations
62pub type Result<T> = std::result::Result<T, CacheError>;
63
64#[cfg(test)]
65mod tests {
66    use super::*;
67
68    #[test]
69    fn test_error_display() {
70        let err = CacheError::NotFound("test_key".to_string());
71        assert_eq!(err.to_string(), "key not found: test_key");
72
73        let err = CacheError::Serialization("failed".to_string());
74        assert_eq!(err.to_string(), "serialization error: failed");
75
76        let err = CacheError::VersionMismatch {
77            expected: 1,
78            actual: 2,
79        };
80        assert_eq!(err.to_string(), "version mismatch: expected 1, got 2");
81    }
82
83    #[test]
84    fn test_error_clone() {
85        let err = CacheError::Timeout;
86        let cloned = err.clone();
87        assert_eq!(err.to_string(), cloned.to_string());
88    }
89}