Skip to main content

xz_memory_core/
error.rs

1use thiserror::Error;
2
3/// Errors for entry store operations.
4#[derive(Error, Debug)]
5pub enum StoreError {
6    /// The underlying storage backend returned an error.
7    #[error("Storage backend error: {0}")]
8    Backend(String),
9
10    /// The requested entry was not found.
11    #[error("Entry not found: {0}")]
12    NotFound(String),
13
14    /// Serialization or deserialization failure.
15    #[error("Serialization error: {0}")]
16    Serialization(String),
17
18    /// Invalid configuration.
19    #[error("Invalid configuration: {0}")]
20    Config(String),
21}
22
23impl StoreError {
24    /// Returns `true` if the operation can be safely retried.
25    pub fn is_retryable(&self) -> bool {
26        match self {
27            StoreError::Backend(_) => true,
28            StoreError::NotFound(_) => false,
29            StoreError::Serialization(_) => false,
30            StoreError::Config(_) => false,
31        }
32    }
33}
34
35#[cfg(test)]
36mod tests {
37    use super::*;
38
39    #[test]
40    fn test_backend_is_retryable() {
41        assert!(StoreError::Backend("transient".into()).is_retryable());
42    }
43
44    #[test]
45    fn test_not_found_not_retryable() {
46        assert!(!StoreError::NotFound("missing".into()).is_retryable());
47    }
48
49    #[test]
50    fn test_debug_display() {
51        let e = StoreError::Backend("db down".into());
52        assert_eq!(format!("{}", e), "Storage backend error: db down");
53    }
54}