Skip to main content

weavatrix_memory/
error.rs

1use core::fmt;
2
3pub type Result<T> = core::result::Result<T, MemoryError>;
4
5#[derive(Debug, Clone, PartialEq, Eq)]
6#[non_exhaustive]
7pub enum MemoryError {
8    InvalidId {
9        kind: &'static str,
10        value: String,
11    },
12    InvalidValue {
13        field: &'static str,
14        reason: &'static str,
15    },
16    VersionConflict {
17        stream: String,
18        expected: Option<u64>,
19        actual: Option<u64>,
20    },
21    DuplicateEvent {
22        id: String,
23    },
24    InvalidReplay {
25        reason: String,
26    },
27    MissingEntity {
28        id: String,
29    },
30    MissingFact {
31        id: String,
32    },
33    ConflictingFact {
34        id: String,
35    },
36    BudgetTooSmall {
37        required: usize,
38        available: usize,
39    },
40    CapacityOverflow,
41    Io {
42        operation: &'static str,
43        message: String,
44    },
45    Codec {
46        message: String,
47    },
48    CorruptLog {
49        offset: u64,
50        reason: String,
51    },
52    ExternalModification,
53    Retrieval {
54        provider: String,
55        message: String,
56    },
57    Extraction {
58        provider: String,
59        message: String,
60    },
61    Graph(String),
62}
63
64impl fmt::Display for MemoryError {
65    fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
66        match self {
67            Self::InvalidId { kind, value } => {
68                write!(formatter, "invalid {kind} identifier: {value:?}")
69            }
70            Self::InvalidValue { field, reason } => {
71                write!(formatter, "invalid {field}: {reason}")
72            }
73            Self::VersionConflict {
74                stream,
75                expected,
76                actual,
77            } => write!(
78                formatter,
79                "version conflict for {stream}: expected {expected:?}, actual {actual:?}"
80            ),
81            Self::DuplicateEvent { id } => write!(formatter, "duplicate event identifier: {id}"),
82            Self::InvalidReplay { reason } => write!(formatter, "invalid replay: {reason}"),
83            Self::MissingEntity { id } => write!(formatter, "missing memory entity: {id}"),
84            Self::MissingFact { id } => write!(formatter, "missing memory fact: {id}"),
85            Self::ConflictingFact { id } => write!(formatter, "conflicting memory fact: {id}"),
86            Self::BudgetTooSmall {
87                required,
88                available,
89            } => write!(
90                formatter,
91                "context budget needs {required} tokens but only {available} are available"
92            ),
93            Self::CapacityOverflow => formatter.write_str("event store capacity exceeded"),
94            Self::Io { operation, message } => {
95                write!(formatter, "I/O error during {operation}: {message}")
96            }
97            Self::Codec { message } => write!(formatter, "codec failed: {message}"),
98            Self::CorruptLog { offset, reason } => {
99                write!(formatter, "corrupt event log at byte {offset}: {reason}")
100            }
101            Self::ExternalModification => {
102                formatter.write_str("event log was modified by another writer")
103            }
104            Self::Retrieval { provider, message } => {
105                write!(formatter, "retrieval provider {provider} failed: {message}")
106            }
107            Self::Extraction { provider, message } => {
108                write!(
109                    formatter,
110                    "extraction provider {provider} failed: {message}"
111                )
112            }
113            Self::Graph(reason) => write!(formatter, "graph projection failed: {reason}"),
114        }
115    }
116}
117
118impl std::error::Error for MemoryError {}
119
120impl From<weavatrix_graph::GraphError> for MemoryError {
121    fn from(value: weavatrix_graph::GraphError) -> Self {
122        Self::Graph(value.to_string())
123    }
124}