manifoldb_storage/engine/
error.rs1use std::fmt;
7
8use thiserror::Error;
9
10#[derive(Debug, Error)]
15pub enum StorageError {
16 #[error("failed to open database: {0}")]
18 Open(String),
19
20 #[error("database not found: {0}")]
22 NotFound(String),
23
24 #[error("table not found: {0}")]
26 TableNotFound(String),
27
28 #[error("key not found")]
30 KeyNotFound,
31
32 #[error("transaction error: {0}")]
34 Transaction(String),
35
36 #[error("cannot write in read-only transaction")]
38 ReadOnly,
39
40 #[error("write conflict: {0}")]
42 Conflict(String),
43
44 #[error("database corruption detected: {0}")]
46 Corruption(String),
47
48 #[error("I/O error: {0}")]
50 Io(#[from] std::io::Error),
51
52 #[error("serialization error: {0}")]
54 Serialization(String),
55
56 #[error("storage full: {0}")]
58 StorageFull(String),
59
60 #[error("invalid argument: {0}")]
62 InvalidArgument(String),
63
64 #[error("operation not supported: {0}")]
66 Unsupported(String),
67
68 #[error("internal error: {0}")]
70 Internal(String),
71}
72
73impl StorageError {
74 #[must_use]
80 pub const fn is_recoverable(&self) -> bool {
81 matches!(self, Self::Conflict(_) | Self::Transaction(_) | Self::Io(_))
82 }
83
84 #[must_use]
86 pub const fn is_not_found(&self) -> bool {
87 matches!(self, Self::NotFound(_) | Self::TableNotFound(_) | Self::KeyNotFound)
88 }
89}
90
91pub type StorageResult<T> = Result<T, StorageError>;
93
94#[derive(Debug)]
96pub struct ErrorContext {
97 pub table: Option<String>,
99 pub key: Option<String>,
101 pub message: Option<String>,
103}
104
105impl fmt::Display for ErrorContext {
106 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
107 let mut parts = Vec::new();
108 if let Some(ref table) = self.table {
109 parts.push(format!("table={table}"));
110 }
111 if let Some(ref key) = self.key {
112 parts.push(format!("key={key}"));
113 }
114 if let Some(ref msg) = self.message {
115 parts.push(msg.clone());
116 }
117 write!(f, "{}", parts.join(", "))
118 }
119}