nodedb_lite/storage/pagedb_storage/errors.rs
1// SPDX-License-Identifier: Apache-2.0
2
3//! `PagedbError` → `LiteError` mapping.
4//!
5//! `PagedbError::NotFound` is **not** mapped here — callers that expect a
6//! missing-key result should convert the `Ok(None)` / empty-vec at the call
7//! site rather than going through the error path.
8//!
9//! `PagedbError::Quota` is mapped to `LiteError::Storage` for now. A dedicated
10//! `LiteError::Quota` variant should be added so that quota pressure is
11//! distinguishable at the application layer without string-matching.
12
13use pagedb::PagedbError;
14
15use crate::error::LiteError;
16
17impl From<PagedbError> for LiteError {
18 fn from(e: PagedbError) -> Self {
19 // Corruption-class errors are typed distinctly so the signal survives
20 // the conversion chain and reaches the caller as a corruption it can
21 // match on, rather than as a generic storage failure.
22 if is_corruption(&e) {
23 return LiteError::Corrupted {
24 detail: format!("pagedb corruption: {e}"),
25 };
26 }
27 match e {
28 PagedbError::Quota { .. } => LiteError::Storage {
29 detail: format!("pagedb quota exceeded: {e}"),
30 },
31 // `Unsupported` is returned by the OPFS VFS shim when the `opfs`
32 // feature is absent, and also by `OpfsVfs::new` if the worker
33 // spawn fails. Surface it as `WorkerFailed` so callers can
34 // distinguish it from generic I/O failures without string-matching.
35 PagedbError::Unsupported => LiteError::WorkerFailed {
36 detail: "pagedb OPFS VFS returned Unsupported — ensure the opfs feature is \
37 enabled and the worker URL is correct"
38 .to_string(),
39 },
40 other => LiteError::Storage {
41 detail: other.to_string(),
42 },
43 }
44 }
45}
46
47/// Returns `true` when the error is a corruption-class error.
48///
49/// Used by `From<PagedbError>` on all targets to type the error as
50/// [`LiteError::Corrupted`], and by the native open path to decide whether the
51/// caller's corruption policy applies.
52pub(crate) fn is_corruption(e: &PagedbError) -> bool {
53 matches!(e, PagedbError::Corruption(_) | PagedbError::ChecksumFailure)
54}