Skip to main content

multi_tier_cache/
error.rs

1use thiserror::Error;
2
3#[cfg(feature = "redis")]
4use redis::RedisError;
5
6/// Result type for cache operations
7pub type CacheResult<T> = std::result::Result<T, CacheError>;
8
9/// Strongly-typed error enum for multi-tier-cache
10#[derive(Error, Debug, Clone)]
11pub enum CacheError {
12    /// Error from a cache backend (Redis, Memcached, etc.)
13    #[error("Backend error: {0}")]
14    BackendError(String),
15    /// Error during serialization/deserialization
16    #[error("Serialization error: {0}")]
17    SerializationError(String),
18    /// Error during cross-instance invalidation
19    #[error("Invalidation error: {0}")]
20    InvalidationError(String),
21    /// Configuration or initialization error
22    #[error("Configuration error: {0}")]
23    ConfigError(String),
24    /// Key not found in cache
25    #[error("Key not found")]
26    NotFound,
27    /// Internal logic error or unexpected state
28    #[error("Internal error: {0}")]
29    InternalError(String),
30}
31
32#[cfg(feature = "redis")]
33impl From<RedisError> for CacheError {
34    fn from(err: RedisError) -> Self {
35        Self::BackendError(err.to_string())
36    }
37}
38
39impl From<serde_json::Error> for CacheError {
40    fn from(err: serde_json::Error) -> Self {
41        Self::SerializationError(err.to_string())
42    }
43}
44
45#[cfg(feature = "msgpack")]
46impl From<rmp_serde::decode::Error> for CacheError {
47    fn from(err: rmp_serde::decode::Error) -> Self {
48        Self::SerializationError(err.to_string())
49    }
50}
51
52impl From<std::num::TryFromIntError> for CacheError {
53    fn from(err: std::num::TryFromIntError) -> Self {
54        Self::ConfigError(err.to_string())
55    }
56}
57
58#[cfg(feature = "backend-memcached")]
59impl From<memcache::MemcacheError> for CacheError {
60    fn from(err: memcache::MemcacheError) -> Self {
61        Self::BackendError(err.to_string())
62    }
63}