Skip to main content

hydracache_core/
error.rs

1use std::error::Error;
2
3/// Errors returned by HydraCache.
4///
5/// # Example
6///
7/// ```rust
8/// use hydracache_core::CacheError;
9///
10/// let error = CacheError::Backend("store unavailable".to_owned());
11/// assert_eq!(error.to_string(), "cache backend error: store unavailable");
12/// ```
13#[derive(Debug, Clone, thiserror::Error)]
14pub enum CacheError {
15    /// Failed to encode a value before storing it.
16    #[error("cache encode error: {0}")]
17    Encode(String),
18
19    /// Failed to decode a value read from the cache.
20    #[error("cache decode error: {0}")]
21    Decode(String),
22
23    /// Loader returned an error.
24    #[error("cache loader error: {0}")]
25    Loader(String),
26
27    /// Backend or internal error.
28    #[error("cache backend error: {0}")]
29    Backend(String),
30}
31
32impl CacheError {
33    /// Wrap a loader error.
34    pub fn loader<E>(source: E) -> Self
35    where
36        E: Error + Send + Sync + 'static,
37    {
38        Self::Loader(source.to_string())
39    }
40}