Skip to main content

ferro_cache/
error.rs

1//! Error types for cache operations.
2
3use thiserror::Error;
4
5/// Cache error types.
6#[derive(Error, Debug)]
7pub enum Error {
8    /// Key not found.
9    #[error("Key not found: {0}")]
10    NotFound(String),
11
12    /// Serialization error.
13    #[error("Serialization error: {0}")]
14    Serialization(String),
15
16    /// Deserialization error.
17    #[error("Deserialization error: {0}")]
18    Deserialization(String),
19
20    /// Connection error.
21    #[error("Connection error: {0}")]
22    Connection(String),
23
24    /// Store not configured.
25    #[error("Store not configured: {0}")]
26    StoreNotConfigured(String),
27
28    /// Redis error.
29    #[cfg(feature = "redis-backend")]
30    #[error("Redis error: {0}")]
31    Redis(#[from] redis::RedisError),
32}
33
34impl Error {
35    /// Create a not found error.
36    pub fn not_found(key: impl Into<String>) -> Self {
37        Self::NotFound(key.into())
38    }
39
40    /// Create a serialization error.
41    pub fn serialization(msg: impl Into<String>) -> Self {
42        Self::Serialization(msg.into())
43    }
44
45    /// Create a deserialization error.
46    pub fn deserialization(msg: impl Into<String>) -> Self {
47        Self::Deserialization(msg.into())
48    }
49
50    /// Create a connection error.
51    pub fn connection(msg: impl Into<String>) -> Self {
52        Self::Connection(msg.into())
53    }
54
55    /// Create a store not configured error.
56    pub fn store_not_configured(store: impl Into<String>) -> Self {
57        Self::StoreNotConfigured(store.into())
58    }
59}