1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
//! Error types for NodeDB-Lite.
mod from_crdt;
mod from_sql;
/// Errors specific to the Lite embedded engine.
#[derive(Debug, thiserror::Error)]
pub enum LiteError {
#[error("storage error: {detail}")]
Storage { detail: String },
#[error("storage backend returned poison lock")]
LockPoisoned,
#[error("async task join failed: {detail}")]
JoinError { detail: String },
#[error("serialization error: {detail}")]
Serialization { detail: String },
#[error("namespace {ns} not recognized")]
InvalidNamespace { ns: u8 },
#[error("bad request: {detail}")]
BadRequest { detail: String },
/// A statement named a collection the engine it addressed does not
/// hold. Maps to `collection_not_found` at the public API boundary.
#[error("{engine} collection '{collection}' does not exist")]
CollectionNotFound {
engine: &'static str,
collection: String,
},
/// A write that would duplicate a declared unique key. Maps to SQLSTATE
/// `23505` at the SQL boundary.
#[error("duplicate key value violates unique constraint on '{collection}': {detail}")]
UniqueViolation { collection: String, detail: String },
/// A write that would store NULL in a NOT NULL column. Maps to SQLSTATE
/// `23502` at the SQL boundary.
#[error("null value in column '{column}' of '{collection}' violates not-null constraint")]
NotNullViolation { collection: String, column: String },
#[error("sync error: {detail}")]
Sync { detail: String },
#[error("query error: {0}")]
Query(String),
#[error("Arrow type conversion: expected {expected}, got {got}")]
ArrowTypeConversion { expected: String, got: String },
#[error("backpressure: {detail}")]
Backpressure { detail: String },
/// Feature or SQL construct not supported in this Lite beta release.
#[error("unsupported: {detail}")]
Unsupported { detail: String },
/// The OPFS worker bridge failed to start or encountered an IPC error.
///
/// This variant is produced when `PagedbStorage::open_opfs` cannot spawn
/// the dedicated Web Worker or when the worker signals a corruption-class
/// failure that cannot be recovered automatically (OPFS has no rename).
#[error("OPFS worker bridge failed: {detail}")]
WorkerFailed { detail: String },
/// An error during key derivation, salt I/O, or encryption setup.
#[error("encryption error: {detail}")]
Encryption { detail: String },
/// A corruption-class failure surfaced from the storage backend.
///
/// Kept as a distinct variant (rather than folded into [`LiteError::Storage`])
/// so the corruption signal survives the error-type conversions and reaches
/// the open-sequence recovery driver, which renames the corrupt store aside,
/// recreates a fresh one, and retries the open exactly once.
#[error("storage corrupted: {detail}")]
Corrupted { detail: String },
/// A full-text index update failed while writing a document.
///
/// The write that triggered it must fail too: the row and its postings are
/// written together, and nothing re-indexes the gap afterwards — the next
/// write to the document only indexes its new text, so a swallowed failure
/// leaves the document permanently missing from (or stale in) the index.
#[error("full-text index update failed for {collection}: {detail}")]
FtsIndex { collection: String, detail: String },
/// A full-text read named a field no document of the collection holds as
/// text, while other fields of the collection are text-indexed. Maps to a
/// bad-request error at the public API boundary.
#[error(
"collection '{collection}' has no full-text index for field '{field}': \
write a document with a string value in '{field}', or search with an \
empty field to cover every string field"
)]
TextIndexMissing { collection: String, field: String },
/// A full-text query the index cannot run, such as a query with only
/// negated terms. Maps to a bad-request error at the public API boundary.
#[error("invalid full-text query on {collection}: {detail}")]
FtsQueryInvalid { collection: String, detail: String },
/// A full-text index read failed. The search fails with it: an empty
/// result would claim that no document matches.
#[error("full-text search failed for {collection}: {detail}")]
FtsSearch { collection: String, detail: String },
/// A KV counter atomic read a stored value it cannot parse as a number,
/// or computed a result out of range. Maps to SQLSTATE `22P02` or `22003`
/// at the SQL boundary, the same as Origin.
#[error("{fault} on {collection}")]
CounterFault {
collection: String,
fault: nodedb_physical::kv_atomic::CounterFault,
},
/// A value of the wrong kind for its target: a KV atomic on a typed row
/// with no column of the type it reads, a text element in a `VECTOR`
/// literal, a scalar write onto a nested CRDT container. `collection` is
/// empty when the fault names no collection. SQLSTATE `42804`.
#[error("{}", type_mismatch_message(.collection, .detail))]
TypeMismatch { collection: String, detail: String },
/// An input the engine cannot compute on: a vector of the wrong
/// dimension, or index input it cannot use. The same public error
/// Origin raises for the same fault, SQLSTATE `22000`.
#[error("{detail}")]
DataException { detail: String },
/// A value past the range of its numeric type: an integer wider than
/// its column, a constant that overflows. SQLSTATE `22003`.
#[error("{detail}")]
NumericValueOutOfRange { detail: String },
/// A statement or CRDT operation named an object that does not exist,
/// such as a document absent at the requested version. SQLSTATE `42704`.
#[error("{object} does not exist")]
UndefinedObject { object: String },
/// The object exists, but not in the state the request needs, such as a
/// CRDT version below the compaction boundary. SQLSTATE `55000`.
#[error("{detail}")]
ObjectNotReady { object: String, detail: String },
/// A named constraint refused a write. `constraint` names the
/// constraint. SQLSTATE class `23`.
#[error("constraint violation: {constraint} on '{collection}': {detail}")]
ConstraintViolation {
collection: String,
constraint: String,
detail: String,
},
/// The request applied nothing it reports as done. The same request
/// succeeds once a transient condition clears: missing causal history
/// arrives, or a full queue drains. Retriable.
#[error("refused, retry: {detail}")]
RetryableRefusal { detail: String },
/// The caller is not allowed to perform the request: expired auth or a
/// delta signature that does not check out.
#[error("authorization denied on {resource}")]
AuthorizationDenied { resource: String },
/// A pre-validation gate refused the request, such as a replayed sync
/// sequence number. The same request fails the same way every time.
#[error("pre-validation rejected: {reason}")]
PrevalidationRejected { reason: String },
}
/// `detail` alone when no collection is named.
fn type_mismatch_message(collection: &str, detail: &str) -> String {
if collection.is_empty() {
detail.to_string()
} else {
format!("type mismatch on {collection}: {detail}")
}
}
impl LiteError {
/// `collection` is not a collection of `engine` (`"strict"`,
/// `"columnar"`).
pub fn collection_not_found(engine: &'static str, collection: impl Into<String>) -> Self {
Self::CollectionNotFound {
engine,
collection: collection.into(),
}
}
/// `e` classified as `From<CrdtError>` classifies it. A storage-class
/// error names `op`, the operation that raised it.
pub(crate) fn crdt(op: &str, e: nodedb_crdt::CrdtError) -> Self {
Self::from(e).within(op)
}
/// `self` with `op` named in a storage-class detail. Every other class
/// keeps its variant and message.
pub(crate) fn within(self, op: &str) -> Self {
match self {
Self::Storage { detail } => Self::Storage {
detail: format!("{op}: {detail}"),
},
other => other,
}
}
}
/// A vector engine error, classified as Origin classifies it: an input
/// vector of the wrong dimension, or index input the engine cannot use, is
/// the caller's data error; a memory budget refusal is backpressure; a
/// stored-data or checkpoint failure is corruption.
impl From<nodedb_vector::error::VectorError> for LiteError {
fn from(e: nodedb_vector::error::VectorError) -> Self {
use nodedb_vector::error::VectorError as Ve;
let detail = e.to_string();
match e {
Ve::DimensionMismatch { .. } | Ve::InvalidInput { .. } => {
Self::DataException { detail }
}
Ve::BudgetExhausted(_) => Self::Backpressure { detail },
Ve::SegmentIo(_) | Ve::InvalidFilterBitmap { .. } => Self::Storage { detail },
// `VectorError` is `#[non_exhaustive]`; every other variant is a
// stored-data or checkpoint failure.
_ => Self::Corrupted { detail },
}
}
}
/// Returns `true` when `e` is the corruption-class variant that should drive
/// the store-recovery retry (rename the corrupt store, recreate fresh, reopen).
pub(crate) fn is_corruption(e: &LiteError) -> bool {
matches!(e, LiteError::Corrupted { .. })
}
/// Expression evaluation failure: division or modulo by a zero divisor, a
/// call to a function no evaluator implements, or a function argument it
/// cannot compute on (vectors of different dimensions, an argument of the
/// wrong type, a malformed JSONPath). SQL requires each to fail the
/// statement rather than fold the row to `NULL`. Mapped to
/// [`LiteError::Query`] so it surfaces to the caller instead of silently
/// filtering rows out. An exact aggregate total out of range is
/// [`LiteError::NumericValueOutOfRange`], the class Origin gives it.
impl From<nodedb_query::EvalError> for LiteError {
fn from(e: nodedb_query::EvalError) -> Self {
use nodedb_query::EvalError as Ee;
match e {
e @ Ee::NumericOverflow { .. } => Self::NumericValueOutOfRange {
detail: e.to_string(),
},
e @ (Ee::DivisionByZero
| Ee::UnknownFunction { .. }
| Ee::VectorDimensionMismatch { .. }
| Ee::ArgumentType { .. }
| Ee::InvalidJsonPath { .. }) => Self::Query(e.to_string()),
}
}
}
impl From<nodedb_types::columnar::SchemaError> for LiteError {
fn from(e: nodedb_types::columnar::SchemaError) -> Self {
Self::BadRequest {
detail: e.to_string(),
}
}
}
impl From<LiteError> for nodedb_types::error::NodeDbError {
fn from(e: LiteError) -> Self {
use nodedb_types::error::{ErrorCode, NodeDbError};
match e {
// The same public errors Origin builds for the same faults.
LiteError::CounterFault { collection, fault } => {
NodeDbError::kv_counter_fault(collection, fault, fault.is_out_of_range())
}
LiteError::TypeMismatch { collection, detail } if collection.is_empty() => {
NodeDbError::from_wire(ErrorCode::TYPE_MISMATCH, detail)
}
LiteError::TypeMismatch { collection, detail } => {
NodeDbError::type_mismatch(collection, detail)
}
LiteError::DataException { detail } => NodeDbError::data_exception(detail),
LiteError::NumericValueOutOfRange { detail } => {
NodeDbError::numeric_value_out_of_range(detail)
}
LiteError::UndefinedObject { object } => NodeDbError::undefined_object(object),
LiteError::ObjectNotReady { object, detail } => {
NodeDbError::object_not_ready(object, detail)
}
LiteError::CollectionNotFound { collection, .. } => {
NodeDbError::collection_not_found(collection)
}
LiteError::UniqueViolation { collection, detail } => {
NodeDbError::constraint_violation(collection, "unique", detail)
}
LiteError::NotNullViolation { collection, column } => {
NodeDbError::constraint_violation(
collection,
"not_null",
format!("null value in column '{column}'"),
)
}
LiteError::ConstraintViolation {
collection,
constraint,
detail,
} => NodeDbError::constraint_violation(collection, constraint, detail),
// Nothing the caller asked for took effect, and the same request
// succeeds later: the retriable class, as Origin renders it.
e @ LiteError::RetryableRefusal { .. } => {
NodeDbError::from_wire(ErrorCode::WRITE_CONFLICT, e.to_string())
}
LiteError::AuthorizationDenied { resource } => {
NodeDbError::authorization_denied(resource)
}
LiteError::PrevalidationRejected { reason } => {
NodeDbError::prevalidation_rejected("crdt", reason)
}
LiteError::Query(detail) => NodeDbError::plan_error(detail),
LiteError::BadRequest { detail } => NodeDbError::bad_request(detail),
// SQLSTATE `0A000`: `SQL_NOT_ENABLED` is the class every
// feature-not-supported refusal carries.
LiteError::Unsupported { detail } => {
NodeDbError::from_wire(ErrorCode::SQL_NOT_ENABLED, detail)
}
e @ (LiteError::TextIndexMissing { .. } | LiteError::FtsQueryInvalid { .. }) => {
NodeDbError::bad_request(e)
}
e if is_corruption(&e) => NodeDbError::segment_corrupted(e.to_string()),
e => NodeDbError::storage(e),
}
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn lite_error_display() {
let e = LiteError::Storage {
detail: "disk full".into(),
};
assert!(e.to_string().contains("disk full"));
}
#[test]
fn lite_error_converts_to_nodedb_error() {
let e = LiteError::Storage {
detail: "test".into(),
};
let ndb: nodedb_types::error::NodeDbError = e.into();
assert!(ndb.to_string().contains("test"));
}
#[test]
fn a_unique_violation_is_a_constraint_violation() {
let e = LiteError::UniqueViolation {
collection: "users".into(),
detail: "key ($.email)=(a@x) already exists".into(),
};
let ndb: nodedb_types::error::NodeDbError = e.into();
assert!(ndb.is_constraint_violation(), "{ndb}");
assert!(ndb.to_string().contains("unique"), "{ndb}");
}
#[test]
fn lite_error_encryption_display_and_convert() {
let e = LiteError::Encryption {
detail: "argon2 key derivation failed".into(),
};
let rendered = e.to_string();
assert!(rendered.contains("encryption error"));
assert!(rendered.contains("argon2 key derivation failed"));
let ndb: nodedb_types::error::NodeDbError = e.into();
assert!(ndb.to_string().contains("argon2 key derivation failed"));
}
#[test]
fn a_missing_collection_is_collection_not_found() {
let e = LiteError::collection_not_found("strict", "users");
assert_eq!(e.to_string(), "strict collection 'users' does not exist");
let ndb: nodedb_types::error::NodeDbError = e.into();
assert_eq!(
ndb.code(),
nodedb_types::error::ErrorCode::COLLECTION_NOT_FOUND,
"{ndb}"
);
}
#[test]
fn a_not_null_violation_is_a_constraint_violation() {
let e = LiteError::NotNullViolation {
collection: "users".into(),
column: "email".into(),
};
let ndb: nodedb_types::error::NodeDbError = e.into();
assert!(ndb.is_constraint_violation(), "{ndb}");
}
#[test]
fn a_bad_request_keeps_its_class() {
let e = LiteError::BadRequest {
detail: "invalid version vector JSON".into(),
};
let ndb: nodedb_types::error::NodeDbError = e.into();
assert_eq!(ndb.code(), nodedb_types::error::ErrorCode::BAD_REQUEST);
}
#[test]
fn a_type_mismatch_without_a_collection_renders_its_detail() {
let e = LiteError::TypeMismatch {
collection: String::new(),
detail: "column 'v': expected a number".into(),
};
assert_eq!(e.to_string(), "column 'v': expected a number");
let ndb: nodedb_types::error::NodeDbError = e.into();
assert_eq!(ndb.code(), nodedb_types::error::ErrorCode::TYPE_MISMATCH);
}
#[test]
fn lite_error_backpressure_display() {
let e = LiteError::Backpressure {
detail: "outbound queue full".into(),
};
assert!(e.to_string().contains("backpressure"));
assert!(e.to_string().contains("outbound queue full"));
}
}