Skip to main content

interstice_core/
error.rs

1use std::fmt;
2
3use interstice_abi::{Authority, Version};
4
5use crate::node::NodeId;
6
7#[derive(Debug)]
8pub enum IntersticeError {
9    // Node
10    NodeNotFound(NodeId),
11    // Authority
12    AuthorityAlreadyTaken(String, String, String),
13    Unauthorized(Authority),
14    // ─── Module / Reducer resolution ──────────────────────────────────────
15    ModuleAlreadyExists(String),
16    ModuleNotFound(String, String),
17    ModuleVersionMismatch(String, String, Version, Version),
18
19    TableNotFound {
20        module_name: String,
21        table_name: String,
22    },
23    ReducerNotFound {
24        module: String,
25        reducer: String,
26    },
27    RowNotFound {
28        primary_key_value: interstice_abi::IndexKey,
29    },
30    UniqueConstraintViolation {
31        table_name: String,
32        field_name: String,
33    },
34    IndexNotFound {
35        table_name: String,
36        field_name: String,
37    },
38    IndexQueryUnsupported {
39        table_name: String,
40        field_name: String,
41    },
42    AutoIncUpdateNotAllowed {
43        table_name: String,
44        field_name: String,
45    },
46    InvalidRow {
47        module: String,
48        table: String,
49    },
50    ReducerCycle {
51        module: String,
52        reducer: String,
53    },
54
55    // ─── WASM loading / linking ────────────────────────────────────────────
56    MissingExport(&'static str),
57    WasmFuncNotFound(String),
58    BadSignature(String),
59    InvalidSchema,
60    AbiVersionMismatch {
61        expected: u16,
62        found: u16,
63    },
64
65    // ─── WASM execution ────────────────────────────────────────────────────
66    WasmTrap(String),
67
68    // ─── Memory handling ───────────────────────────────────────────────────
69    MemoryRead,
70    MemoryWrite,
71
72    // Network
73    NetworkSendFailed,
74    UnknownPeer,
75    ProtocolError(String),
76
77    // ─── Internal invariants ───────────────────────────────────────────────
78    Internal(String),
79}
80
81impl fmt::Display for IntersticeError {
82    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
83        use IntersticeError::*;
84
85        match self {
86            NodeNotFound(node_id) => {
87                write!(f, "Node '{}' not found", node_id)
88            }
89            ModuleVersionMismatch(
90                module_name,
91                dependency_module_name,
92                req_version,
93                actual_version,
94            ) => {
95                write!(
96                    f,
97                    "module '{}' requires module {} with version {} but is {}",
98                    module_name,
99                    dependency_module_name,
100                    Into::<String>::into(req_version.clone()),
101                    Into::<String>::into(actual_version.clone())
102                )
103            }
104            ModuleAlreadyExists(name) => {
105                write!(f, "module '{}' already exists", name)
106            }
107            AuthorityAlreadyTaken(name, authority, in_place_module_name) => {
108                write!(
109                    f,
110                    "module '{}' require already taken authority {} by module {}",
111                    name, authority, in_place_module_name
112                )
113            }
114            Unauthorized(authority) => {
115                write!(f, "module does not have {:?} authority", authority)
116            }
117            ModuleNotFound(name, context) => {
118                write!(f, "module '{}' not found. {}", name, context)
119            }
120            RowNotFound { primary_key_value } => {
121                write!(
122                    f,
123                    "row with primary key value {:?} not found",
124                    primary_key_value
125                )
126            }
127            UniqueConstraintViolation {
128                table_name,
129                field_name,
130            } => {
131                write!(
132                    f,
133                    "unique constraint violation on table '{}' field '{}'",
134                    table_name, field_name
135                )
136            }
137            IndexNotFound {
138                table_name,
139                field_name,
140            } => {
141                write!(
142                    f,
143                    "index not found on table '{}' field '{}'",
144                    table_name, field_name
145                )
146            }
147            IndexQueryUnsupported {
148                table_name,
149                field_name,
150            } => {
151                write!(
152                    f,
153                    "index query not supported for table '{}' field '{}'",
154                    table_name, field_name
155                )
156            }
157            AutoIncUpdateNotAllowed {
158                table_name,
159                field_name,
160            } => {
161                write!(
162                    f,
163                    "auto_inc field '{}' on table '{}' cannot be updated",
164                    field_name, table_name
165                )
166            }
167            TableNotFound {
168                module_name: module,
169                table_name: table,
170            } => {
171                write!(f, "table '{}' not found in module '{}'", table, module)
172            }
173            ReducerNotFound { module, reducer } => {
174                write!(f, "reducer '{}' not found in module '{}'", reducer, module)
175            }
176            InvalidRow { module, table } => {
177                write!(
178                    f,
179                    "invalid row encountered on transaction in module {} for table '{}'",
180                    module, table
181                )
182            }
183            ReducerCycle { module, reducer } => {
184                write!(
185                    f,
186                    "reducer cycle detected while calling '{}::{}'",
187                    module, reducer
188                )
189            }
190            MissingExport(name) => {
191                write!(f, "missing required wasm export '{}'", name)
192            }
193            WasmFuncNotFound(name) => {
194                write!(f, "wasm function '{}' not found", name)
195            }
196            BadSignature(name) => {
197                write!(f, "invalid wasm signature for '{}'", name)
198            }
199            InvalidSchema => {
200                write!(f, "invalid module schema")
201            }
202            AbiVersionMismatch { expected, found } => {
203                write!(
204                    f,
205                    "ABI version mismatch: expected {}, found {}",
206                    expected, found
207                )
208            }
209            WasmTrap(msg) => {
210                write!(f, "wasm trapped: {}", msg)
211            }
212            MemoryRead => {
213                write!(f, "failed to read from wasm memory")
214            }
215            MemoryWrite => {
216                write!(f, "failed to write to wasm memory")
217            }
218            NetworkSendFailed => {
219                write!(f, "failed to send packet")
220            }
221            UnknownPeer => {
222                write!(f, "failed to find peer")
223            }
224            ProtocolError(msg) => {
225                write!(f, "Network protocol error: {msg}")
226            }
227            Internal(msg) => {
228                write!(f, "internal error: {}", msg)
229            }
230        }
231    }
232}
233
234impl std::error::Error for IntersticeError {}