Skip to main content

siderite_cache/
error.rs

1//! [`CacheError`] and conversion helpers.
2
3use thiserror::Error;
4
5/// Failure from a [`Cache`](crate::Cache) operation.
6///
7/// Messages never include cached values, passwords, tokens, API keys or other
8/// secrets. Keys may appear because they identify the entry; do not put
9/// secrets in cache keys.
10#[derive(Debug, Error)]
11#[non_exhaustive]
12pub enum CacheError {
13    /// Configured key, value or total byte admission was exceeded.
14    #[error("cache byte admission limit exceeded")]
15    SizeLimit,
16    /// Cache limits must be positive and fit within the total byte budget.
17    #[error("invalid cache byte limits")]
18    InvalidLimits,
19    /// A TTL is zero where a positive lifetime is required, or cannot be
20    /// represented by the backend's monotonic deadline.
21    #[error("cache TTL is not representable")]
22    InvalidTtl,
23    /// The backend does not implement an optional [`Cache`](crate::Cache)
24    /// operation. Callers must fail open or bypass; they must not emulate
25    /// the operation with non-atomic calls.
26    #[error("cache operation `{operation}` is unsupported by this backend")]
27    Unsupported {
28        /// Name of the unsupported trait method.
29        operation: &'static str,
30    },
31    /// The backend could not complete the operation.
32    #[error("cache backend error: {0}")]
33    Backend(String),
34    /// [`Cache::increment`](crate::Cache::increment) found a non-integer value,
35    /// or the result would overflow a signed 64-bit integer.
36    #[error("cache key `{key}` is not an integer or the increment overflowed")]
37    NotInteger {
38        /// The cache key (not the stored value).
39        key: String,
40    },
41    /// JSON serialization failed before anything was stored.
42    #[error("cannot serialize cache value for `{key}`: {reason}")]
43    Serialize {
44        /// The cache key.
45        key: String,
46        /// Why serialization failed.
47        reason: String,
48    },
49    /// A stored value could not be decoded as JSON of the requested type.
50    #[error("cannot deserialize cache value for `{key}`: {reason}")]
51    Deserialize {
52        /// The cache key.
53        key: String,
54        /// Why deserialization failed.
55        reason: String,
56    },
57}