Skip to main content

threatflux_cache/
error.rs

1//! Error types for the cache library
2
3use std::io;
4use thiserror::Error;
5
6/// Main error type for cache operations
7#[derive(Error, Debug)]
8pub enum CacheError {
9    /// I/O error occurred during cache operations
10    #[error("I/O error: {0}")]
11    Io(#[from] io::Error),
12
13    /// Serialization error
14    #[error("Serialization error: {0}")]
15    Serialization(String),
16
17    /// Deserialization error
18    #[error("Deserialization error: {0}")]
19    Deserialization(String),
20
21    /// Cache capacity exceeded
22    #[error("Cache capacity exceeded: {message}")]
23    CapacityExceeded {
24        /// Error message
25        message: String,
26    },
27
28    /// Storage backend error
29    #[error("Storage backend error: {0}")]
30    StorageBackend(String),
31
32    /// Entry not found
33    #[error("Entry not found for key")]
34    NotFound,
35
36    /// Invalid configuration
37    #[error("Invalid configuration: {0}")]
38    InvalidConfiguration(String),
39
40    /// Compression error
41    #[cfg(feature = "compression")]
42    #[error("Compression error: {0}")]
43    Compression(String),
44
45    /// Custom error for extensions
46    #[error("Custom error: {0}")]
47    Custom(String),
48}
49
50/// Result type alias for cache operations
51pub type Result<T> = std::result::Result<T, CacheError>;
52
53// Implement conversions for common serialization errors
54#[cfg(feature = "json-serialization")]
55impl From<serde_json::Error> for CacheError {
56    fn from(err: serde_json::Error) -> Self {
57        if err.is_data() || err.is_eof() {
58            CacheError::Deserialization(err.to_string())
59        } else {
60            CacheError::Serialization(err.to_string())
61        }
62    }
63}
64
65#[cfg(feature = "bincode-serialization")]
66impl From<bincode::Error> for CacheError {
67    fn from(err: bincode::Error) -> Self {
68        CacheError::Serialization(err.to_string())
69    }
70}