Skip to main content

sui_graph_store/
error.rs

1//! Typed errors for the graph store.
2//!
3//! Every fallible operation returns [`Result<T>`]. The variants are
4//! deliberately specific so callers can react (`HashMismatch` should
5//! drop the cache entry and re-fetch; `NotFound` is benign on
6//! lookup-or-compute paths).
7
8use std::io;
9use std::path::PathBuf;
10
11use crate::content_address::GraphHash;
12
13pub type Result<T> = std::result::Result<T, Error>;
14
15#[derive(Debug, thiserror::Error)]
16pub enum Error {
17    #[error("graph store I/O at {path}: {source}")]
18    Io {
19        path: PathBuf,
20        #[source]
21        source: io::Error,
22    },
23
24    #[error("redb database error: {0}")]
25    Db(#[from] redb::DatabaseError),
26
27    #[error("redb transaction error: {0}")]
28    Transaction(#[from] redb::TransactionError),
29
30    #[error("redb table error: {0}")]
31    Table(#[from] redb::TableError),
32
33    #[error("redb storage error: {0}")]
34    Storage(#[from] redb::StorageError),
35
36    #[error("redb commit error: {0}")]
37    Commit(#[from] redb::CommitError),
38
39    /// Asked for a hash the index has no record of.
40    #[error("graph not found: {hash}")]
41    NotFound { hash: GraphHash },
42
43    /// The blob file's BLAKE3 didn't match the expected hash. Index entry
44    /// is stale or the blob was tampered with — caller should drop the
45    /// entry and re-fetch from upstream.
46    #[error("hash mismatch on blob {expected}: bytes hash to {actual}")]
47    HashMismatch {
48        expected: GraphHash,
49        actual: GraphHash,
50    },
51
52    /// rkyv validation rejected the archive shape. The blob is structurally
53    /// invalid for the requested type — never write what the typed border
54    /// can't read.
55    #[error("rkyv validation failed for blob {hash}: {message}")]
56    Validation { hash: GraphHash, message: String },
57
58    /// Caller passed a malformed [`GraphHash`] (wrong length / wrong
59    /// base32 alphabet).
60    #[error("invalid graph hash {input:?}: {reason}")]
61    BadHash { input: String, reason: &'static str },
62}
63
64impl Error {
65    pub fn io(path: impl Into<PathBuf>, source: io::Error) -> Self {
66        Self::Io {
67            path: path.into(),
68            source,
69        }
70    }
71}