datumesh_engine/error.rs
1//! The engine's errors: [`EvalError`] (what a runtime and [`Inputs`](crate::Inputs) see),
2//! [`EngineError`] (what [`Engine::get`](crate::Engine::get) returns), [`ConfigError`]
3//! (building an engine) and [`StoreError`] (a store failure, in the engine's words).
4
5use std::fmt;
6use std::sync::Arc;
7
8/// A store failure, as the engine reports it.
9///
10/// It wraps the store's error (its [`source`](std::error::Error::source)), with the engine's
11/// own wording: the store's `Conflict` speaks of signatures, which is not what a conflict
12/// means to the engine (another receipt is already recorded for a version).
13#[derive(Clone, Debug)]
14pub struct StoreError {
15 op: &'static str,
16 inner: Arc<datumesh_store::Error>,
17}
18
19impl StoreError {
20 pub(crate) fn new(op: &'static str, e: datumesh_store::Error) -> StoreError {
21 StoreError { op, inner: Arc::new(e) }
22 }
23
24 /// The store operation that failed (`stored_asof`, `put_receipt`, ...).
25 pub fn operation(&self) -> &str {
26 self.op
27 }
28
29 /// The store's error.
30 pub fn store_error(&self) -> &datumesh_store::Error {
31 &self.inner
32 }
33
34 /// Whether the store refused a write because another receipt is recorded under the same
35 /// key.
36 pub fn is_conflict(&self) -> bool {
37 matches!(*self.inner, datumesh_store::Error::Conflict { .. })
38 }
39}
40
41impl fmt::Display for StoreError {
42 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
43 match &*self.inner {
44 datumesh_store::Error::Conflict { hash } => write!(
45 f,
46 "store {}: another receipt is already recorded where receipt {hash} was to go; the recorded one is kept",
47 self.op
48 ),
49 other => write!(f, "store {}: {other}", self.op),
50 }
51 }
52}
53
54impl std::error::Error for StoreError {
55 fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
56 Some(&*self.inner)
57 }
58}
59
60/// What evaluating a function can fail with: returned by [`Inputs::read`](crate::Inputs::read)
61/// and [`Budget::charge`](crate::Budget::charge), and by a [`Runtime`](crate::Runtime).
62///
63/// A runtime propagates every error it gets from `Inputs::read` or `Budget::charge`
64/// unchanged (with `?`). The engine does not depend on it: it records each such error when it
65/// happens, and what the runtime returns after it is ignored.
66#[non_exhaustive]
67#[derive(Clone, Debug)]
68pub enum EvalError {
69 /// No runtime is configured for the function's `code.lang`.
70 NoRuntime {
71 /// The language.
72 lang: String,
73 },
74 /// The function read a function its descriptor does not declare in `inputs`.
75 UndeclaredInput {
76 /// The fully qualified name read.
77 fn_name: String,
78 },
79 /// The cell reappeared on the evaluation stack: it depends on itself.
80 Cycle {
81 /// The cell's id.
82 cell_id: String,
83 },
84 /// The evaluation stack reached the engine's depth limit.
85 DepthExceeded {
86 /// The limit.
87 limit: usize,
88 },
89 /// The request's fuel (runtime steps plus input reads) ran out.
90 BudgetExceeded {
91 /// The fuel the request had.
92 fuel: u64,
93 },
94 /// A function name has no binding as of the evaluation's asof.
95 NameUnresolved {
96 /// The fully qualified name.
97 fn_name: String,
98 /// The asof.
99 asof: String,
100 },
101 /// The store failed.
102 Store(StoreError),
103 /// The runtime failed: its own message.
104 Runtime(String),
105 /// The input read is not available yet: the engine evaluates it, then runs the reading
106 /// function again from the start (see [`Inputs`](crate::Inputs)). Added to the brief's
107 /// list: it is how evaluation suspends without native recursion.
108 Suspended,
109 /// The input has no value as of the evaluation's asof (a stored cell with no version
110 /// answering then, or a computed cell one of whose inputs has none). The reading cell has
111 /// no value either ([`Answer::NotCovered`](crate::Answer::NotCovered)), even if the
112 /// runtime catches it: a receipt lists the inputs read, and cannot list an absence, so a
113 /// value computed from one could be neither checked against its inputs nor recomputed.
114 /// Added to the brief's list.
115 InputNotCovered {
116 /// The input cell's id.
117 cell_id: String,
118 },
119 /// The arguments given to `Inputs::read` do not make a cell of the input function (a
120 /// slice, an unknown or missing argument, a type that differs from the signature). Added to
121 /// the brief's list: it is the runtime's mistake, not the engine's or the store's.
122 InvalidInput(datumesh_core::Error),
123}
124
125impl fmt::Display for EvalError {
126 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
127 match self {
128 EvalError::NoRuntime { lang } => write!(f, "no runtime for language {lang}"),
129 EvalError::UndeclaredInput { fn_name } => write!(f, "{fn_name} is not a declared input"),
130 EvalError::Cycle { cell_id } => write!(f, "cycle: cell {cell_id} depends on itself"),
131 EvalError::DepthExceeded { limit } => write!(f, "evaluation deeper than the limit of {limit}"),
132 EvalError::BudgetExceeded { fuel } => write!(f, "evaluation needs more than its fuel of {fuel}"),
133 EvalError::NameUnresolved { fn_name, asof } => write!(f, "{fn_name} has no binding as of {asof}"),
134 EvalError::Store(e) => e.fmt(f),
135 EvalError::Runtime(m) => write!(f, "runtime: {m}"),
136 EvalError::Suspended => f.write_str("evaluation suspended until an input is available"),
137 EvalError::InputNotCovered { cell_id } => write!(f, "input cell {cell_id} has no value as of the asof"),
138 EvalError::InvalidInput(e) => write!(f, "invalid input cell: {e}"),
139 }
140 }
141}
142
143impl std::error::Error for EvalError {
144 fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
145 match self {
146 EvalError::Store(e) => Some(e),
147 EvalError::InvalidInput(e) => Some(e),
148 _ => None,
149 }
150 }
151}
152
153/// What [`Engine::get`](crate::Engine::get) can fail with. For how each maps to a response
154/// envelope, see the crate documentation.
155#[non_exhaustive]
156#[derive(Clone, Debug)]
157pub enum EngineError {
158 /// No runtime is configured for a function's `code.lang`.
159 NoRuntime {
160 /// The language.
161 lang: String,
162 /// The function's hash.
163 fn_hash: String,
164 },
165 /// A function read a function its descriptor does not declare in `inputs`.
166 UndeclaredInput {
167 /// The fully qualified name read.
168 fn_name: String,
169 },
170 /// A cell depends on itself.
171 Cycle {
172 /// The cell that reappeared on the evaluation stack.
173 cell_id: String,
174 },
175 /// The evaluation stack reached the depth limit.
176 DepthExceeded {
177 /// The limit.
178 limit: usize,
179 },
180 /// The request's fuel ran out. Nothing was stored for the request.
181 BudgetExceeded {
182 /// The fuel the request had.
183 fuel: u64,
184 },
185 /// An input's function name has no binding as of the request's asof.
186 NameUnresolved {
187 /// The fully qualified name.
188 fn_name: String,
189 /// The asof.
190 asof: String,
191 },
192 /// The store failed.
193 Store(StoreError),
194 /// A runtime failed, or broke its contract (returned a value of another type than the
195 /// function declares, or `Suspended` without a pending input).
196 Runtime {
197 /// The cell being evaluated.
198 cell_id: String,
199 /// What happened.
200 message: String,
201 },
202 /// The request is malformed: an asof that is not an instant, a slice, arguments that do
203 /// not match the function's signature; or a runtime read such an input. The core's error
204 /// carries the code (`E_CANON_INSTANT`, `E_SLICE`, `E_ARG_UNKNOWN`, ...). Added to the
205 /// brief's list.
206 Invalid(datumesh_core::Error),
207 /// A computed receipt could not be signed: the core refused its members. In practice an
208 /// input whose `asof` is later than the engine's clock (`E_TIME_ORDER`: `asof <= issued`).
209 /// Added to the brief's list.
210 Sign(datumesh_core::Error),
211}
212
213impl fmt::Display for EngineError {
214 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
215 match self {
216 EngineError::NoRuntime { lang, fn_hash } => {
217 write!(f, "no runtime for language {lang} (function {fn_hash})")
218 }
219 EngineError::UndeclaredInput { fn_name } => write!(f, "{fn_name} is not a declared input"),
220 EngineError::Cycle { cell_id } => write!(f, "cycle: cell {cell_id} depends on itself"),
221 EngineError::DepthExceeded { limit } => write!(f, "evaluation deeper than the limit of {limit}"),
222 EngineError::BudgetExceeded { fuel } => {
223 write!(f, "evaluation needs more than its fuel of {fuel}; nothing was stored")
224 }
225 EngineError::NameUnresolved { fn_name, asof } => write!(f, "{fn_name} has no binding as of {asof}"),
226 EngineError::Store(e) => e.fmt(f),
227 EngineError::Runtime { cell_id, message } => write!(f, "runtime, evaluating cell {cell_id}: {message}"),
228 EngineError::Invalid(e) => write!(f, "invalid request: {e}"),
229 EngineError::Sign(e) => write!(f, "cannot sign the computed receipt: {e}"),
230 }
231 }
232}
233
234impl std::error::Error for EngineError {
235 fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
236 match self {
237 EngineError::Store(e) => Some(e),
238 EngineError::Invalid(e) | EngineError::Sign(e) => Some(e),
239 _ => None,
240 }
241 }
242}
243
244impl From<StoreError> for EngineError {
245 fn from(e: StoreError) -> EngineError {
246 EngineError::Store(e)
247 }
248}
249
250/// Why an engine could not be built ([`EngineBuilder::build`](crate::EngineBuilder::build)).
251#[non_exhaustive]
252#[derive(Debug)]
253pub enum ConfigError {
254 /// The mesh host is not a DNS name.
255 Host(datumesh_core::Error),
256 /// No signing key was given.
257 NoSigningKey,
258 /// The signing key's seed file could not be read, or does not hold a seed (32 bytes, or
259 /// 64 hex digits with an optional final newline).
260 KeyFile(String),
261 /// The licence for computed receipts is not an SPDX identifier or an https URL.
262 Licence(datumesh_core::Error),
263 /// The store holds no key document for the mesh host, or the document has no key with the
264 /// signing key's id and public key: the store would refuse every computed receipt.
265 KeyNotPublished {
266 /// The mesh host.
267 host: String,
268 /// The signing key's id.
269 key_id: String,
270 },
271 /// Reading the mesh host's key document from the store failed.
272 Store(StoreError),
273 /// Two runtimes for one language, or a runtime for `stored`, which the engine answers
274 /// itself.
275 Runtime(String),
276}
277
278impl fmt::Display for ConfigError {
279 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
280 match self {
281 ConfigError::Host(e) => write!(f, "mesh host: {e}"),
282 ConfigError::NoSigningKey => f.write_str("no signing key"),
283 ConfigError::KeyFile(m) => write!(f, "signing key file: {m}"),
284 ConfigError::Licence(e) => write!(f, "licence: {e}"),
285 ConfigError::KeyNotPublished { host, key_id } => {
286 write!(f, "the store holds no key document of {host} publishing key {key_id}")
287 }
288 ConfigError::Store(e) => e.fmt(f),
289 ConfigError::Runtime(m) => write!(f, "runtimes: {m}"),
290 }
291 }
292}
293
294impl std::error::Error for ConfigError {
295 fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
296 match self {
297 ConfigError::Host(e) | ConfigError::Licence(e) => Some(e),
298 ConfigError::Store(e) => Some(e),
299 _ => None,
300 }
301 }
302}