Skip to main content

tokio_memory/
error.rs

1//! # Error types for tokio-memory
2//!
3//! All errors are named, typed, and propagatable. Every variant must have
4//! at least one test that triggers it (see module-level tests below).
5
6use crate::id::MemoryId;
7
8/// The unified error type for all tokio-memory operations.
9///
10/// # Variants
11/// Each variant corresponds to a distinct failure mode. Use `?` to propagate
12/// these errors; do not use `unwrap()` or `expect()` in production paths.
13#[derive(Debug, thiserror::Error)]
14pub enum MemoryError {
15    /// A memory item with the given [`MemoryId`] was not found in the store.
16    #[error("Memory item '{0}' not found")]
17    NotFound(MemoryId),
18
19    /// The working memory buffer has reached its configured capacity.
20    #[error("Working memory buffer is full (capacity: {capacity})")]
21    BufferFull { capacity: usize },
22
23    /// Binary serialization or deserialization failed.
24    #[error("Serialization failed: {0}")]
25    Serialization(String),
26
27    /// A confidence value was outside the valid range `[0.0, 1.0]`.
28    #[error("Invalid confidence value {value}: must be in [0.0, 1.0]")]
29    InvalidConfidence { value: f32 },
30
31    /// The consolidation pipeline could not process an episode into facts.
32    #[error("Consolidation failed: {reason}")]
33    ConsolidationFailed { reason: String },
34
35    /// A conflict resolver rejected the incoming fact; the existing fact is retained.
36    #[error("Conflict resolution rejected incoming fact")]
37    ConflictRejected,
38
39    /// A persistence-layer operation (snapshot I/O, codec) failed.
40    #[error("Persistence error: {0}")]
41    Persistence(String),
42}
43
44#[cfg(test)]
45mod tests {
46    use super::*;
47    use crate::id::MemoryId;
48
49    #[test]
50    fn test_not_found_error_displays_id() {
51        let id = MemoryId::new();
52        let err = MemoryError::NotFound(id.clone());
53        let msg = err.to_string();
54        assert!(msg.contains(&id.to_string()), "expected id in message: {msg}");
55    }
56
57    #[test]
58    fn test_buffer_full_error_displays_capacity() {
59        let err = MemoryError::BufferFull { capacity: 42 };
60        assert!(err.to_string().contains("42"));
61    }
62
63    #[test]
64    fn test_invalid_confidence_error_displays_value() {
65        let err = MemoryError::InvalidConfidence { value: 1.5 };
66        assert!(err.to_string().contains("1.5"));
67    }
68
69    #[test]
70    fn test_consolidation_failed_error_displays_reason() {
71        let err = MemoryError::ConsolidationFailed {
72            reason: "no policies matched".into(),
73        };
74        assert!(err.to_string().contains("no policies matched"));
75    }
76
77    #[test]
78    fn test_conflict_rejected_error_has_message() {
79        let err = MemoryError::ConflictRejected;
80        assert!(!err.to_string().is_empty());
81    }
82
83    #[test]
84    fn test_persistence_error_displays_message() {
85        let err = MemoryError::Persistence("disk full".into());
86        assert!(err.to_string().contains("disk full"));
87    }
88
89    #[test]
90    fn test_serialization_error_display() {
91        let mem_err = MemoryError::Serialization("postcard: unexpected end of input".into());
92        assert!(mem_err.to_string().contains("Serialization"));
93    }
94}