Development Documentation (main branch) - For stable release docs, see docs.rs/eidetica

eidetica/store/
errors.rs

1//! Generic error types for store operations.
2//!
3//! This module defines generic error types that can be used by any store implementation.
4//! Specific store types should define their own error types for implementation-specific errors.
5
6use thiserror::Error;
7
8use crate::Error;
9
10/// Generic error types for store operations.
11///
12/// This enum provides fundamental error variants that apply to any store implementation.
13/// Specific store types (DocStore, Table, etc.) should define their own error types
14/// for implementation-specific errors and convert them to SubtreeError when needed.
15#[non_exhaustive]
16#[derive(Debug, Error)]
17pub enum StoreError {
18    /// Key or record not found in store
19    #[error("Key not found in store '{store}': {key}")]
20    KeyNotFound { store: String, key: String },
21
22    /// Serialization failed for store data
23    #[error("Serialization failed in store '{store}': {reason}")]
24    SerializationFailed { store: String, reason: String },
25
26    /// Deserialization failed for store data
27    #[error("Deserialization failed in store '{store}': {reason}")]
28    DeserializationFailed { store: String, reason: String },
29
30    /// Type mismatch in store operation
31    #[error("Type mismatch in store '{store}': expected {expected}, found {actual}")]
32    TypeMismatch {
33        store: String,
34        expected: String,
35        actual: String,
36    },
37
38    /// Invalid operation for the store type
39    #[error("Invalid operation '{operation}' for store '{store}': {reason}")]
40    InvalidOperation {
41        store: String,
42        operation: String,
43        reason: String,
44    },
45
46    /// Store operation requires transaction context
47    #[error("Operation requires transaction context for store '{store}'")]
48    RequiresTransaction { store: String },
49
50    /// Data corruption detected in store
51    #[error("Data corruption detected in store '{store}': {reason}")]
52    DataCorruption { store: String, reason: String },
53
54    /// Invalid configuration for the store
55    #[error("Invalid configuration for store '{store}': {reason}")]
56    InvalidConfiguration { store: String, reason: String },
57
58    /// Implementation-specific error from a store type
59    #[error("Store implementation error in '{store}': {reason}")]
60    ImplementationError { store: String, reason: String },
61}
62
63impl StoreError {
64    /// Check if this error indicates a resource was not found
65    pub fn is_not_found(&self) -> bool {
66        matches!(self, StoreError::KeyNotFound { .. })
67    }
68
69    /// Check if this error is related to serialization
70    pub fn is_serialization_error(&self) -> bool {
71        matches!(
72            self,
73            StoreError::SerializationFailed { .. } | StoreError::DeserializationFailed { .. }
74        )
75    }
76
77    /// Check if this error is related to type mismatches
78    pub fn is_type_error(&self) -> bool {
79        matches!(self, StoreError::TypeMismatch { .. })
80    }
81
82    /// Check if this error is related to data integrity
83    pub fn is_integrity_error(&self) -> bool {
84        matches!(self, StoreError::DataCorruption { .. })
85    }
86
87    /// Check if this error is related to invalid operations
88    pub fn is_operation_error(&self) -> bool {
89        matches!(
90            self,
91            StoreError::InvalidOperation { .. } | StoreError::RequiresTransaction { .. }
92        )
93    }
94
95    /// Check if this error is implementation-specific
96    pub fn is_implementation_error(&self) -> bool {
97        matches!(self, StoreError::ImplementationError { .. })
98    }
99
100    /// Get the store name associated with this error
101    pub fn store_name(&self) -> &str {
102        match self {
103            StoreError::KeyNotFound { store, .. }
104            | StoreError::SerializationFailed { store, .. }
105            | StoreError::DeserializationFailed { store, .. }
106            | StoreError::TypeMismatch { store, .. }
107            | StoreError::InvalidOperation { store, .. }
108            | StoreError::RequiresTransaction { store, .. }
109            | StoreError::DataCorruption { store, .. }
110            | StoreError::InvalidConfiguration { store, .. }
111            | StoreError::ImplementationError { store, .. } => store,
112        }
113    }
114
115    /// Get the operation name if this is an operation-specific error
116    pub fn operation(&self) -> Option<&str> {
117        match self {
118            StoreError::InvalidOperation { operation, .. } => Some(operation),
119            _ => None,
120        }
121    }
122
123    /// Get the key if this is a key-related error
124    pub fn key(&self) -> Option<&str> {
125        match self {
126            StoreError::KeyNotFound { key, .. } => Some(key),
127            _ => None,
128        }
129    }
130}
131
132// Conversion from SubtreeError to the main Error type
133impl From<StoreError> for Error {
134    fn from(err: StoreError) -> Self {
135        Error::Store(err)
136    }
137}