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    InvalidRow {
28        module: String,
29        table: String,
30    },
31    ReducerCycle {
32        module: String,
33        reducer: String,
34    },
35
36    // ─── WASM loading / linking ────────────────────────────────────────────
37    MissingExport(&'static str),
38    WasmFuncNotFound(String),
39    BadSignature(String),
40    InvalidSchema,
41    AbiVersionMismatch {
42        expected: u16,
43        found: u16,
44    },
45
46    // ─── WASM execution ────────────────────────────────────────────────────
47    WasmTrap(String),
48
49    // ─── Memory handling ───────────────────────────────────────────────────
50    MemoryRead,
51    MemoryWrite,
52
53    // Network
54    NetworkSendFailed,
55    UnknownPeer,
56    ProtocolError(String),
57
58    // ─── Internal invariants ───────────────────────────────────────────────
59    Internal(String),
60}
61
62impl fmt::Display for IntersticeError {
63    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
64        use IntersticeError::*;
65
66        match self {
67            NodeNotFound(node_id) => {
68                write!(f, "Node '{}' not found", node_id)
69            }
70            ModuleVersionMismatch(
71                module_name,
72                dependency_module_name,
73                req_version,
74                actual_version,
75            ) => {
76                write!(
77                    f,
78                    "module '{}' requires module {} with version {} but is {}",
79                    module_name,
80                    dependency_module_name,
81                    Into::<String>::into(req_version.clone()),
82                    Into::<String>::into(actual_version.clone())
83                )
84            }
85            ModuleAlreadyExists(name) => {
86                write!(f, "module '{}' already exists", name)
87            }
88            AuthorityAlreadyTaken(name, authority, in_place_module_name) => {
89                write!(
90                    f,
91                    "module '{}' require already taken authority {} by module {}",
92                    name, authority, in_place_module_name
93                )
94            }
95            Unauthorized(authority) => {
96                write!(f, "module does not have {:?} authority", authority)
97            }
98            ModuleNotFound(name, context) => {
99                write!(f, "module '{}' not found. {}", name, context)
100            }
101            TableNotFound {
102                module_name: module,
103                table_name: table,
104            } => {
105                write!(f, "table '{}' not found in module '{}'", table, module)
106            }
107            ReducerNotFound { module, reducer } => {
108                write!(f, "reducer '{}' not found in module '{}'", reducer, module)
109            }
110            InvalidRow { module, table } => {
111                write!(
112                    f,
113                    "invalid row encountered on transaction in module {} for table '{}'",
114                    module, table
115                )
116            }
117            ReducerCycle { module, reducer } => {
118                write!(
119                    f,
120                    "reducer cycle detected while calling '{}::{}'",
121                    module, reducer
122                )
123            }
124            MissingExport(name) => {
125                write!(f, "missing required wasm export '{}'", name)
126            }
127            WasmFuncNotFound(name) => {
128                write!(f, "wasm function '{}' not found", name)
129            }
130            BadSignature(name) => {
131                write!(f, "invalid wasm signature for '{}'", name)
132            }
133            InvalidSchema => {
134                write!(f, "invalid module schema")
135            }
136            AbiVersionMismatch { expected, found } => {
137                write!(
138                    f,
139                    "ABI version mismatch: expected {}, found {}",
140                    expected, found
141                )
142            }
143            WasmTrap(msg) => {
144                write!(f, "wasm trapped: {}", msg)
145            }
146            MemoryRead => {
147                write!(f, "failed to read from wasm memory")
148            }
149            MemoryWrite => {
150                write!(f, "failed to write to wasm memory")
151            }
152            NetworkSendFailed => {
153                write!(f, "failed to send packet")
154            }
155            UnknownPeer => {
156                write!(f, "failed to find peer")
157            }
158            ProtocolError(msg) => {
159                write!(f, "Network protocol error: {msg}")
160            }
161            Internal(msg) => {
162                write!(f, "internal error: {}", msg)
163            }
164        }
165    }
166}
167
168impl std::error::Error for IntersticeError {}