Skip to main content

sapphire_framework_sync/
error.rs

1//! Error type for the replication core.
2
3use crate::hash::ContentHash;
4use crate::report::PauseReason;
5
6/// Errors raised by the replication core.
7#[derive(Debug, thiserror::Error)]
8pub enum Error {
9    #[error("I/O error: {0}")]
10    Io(#[from] std::io::Error),
11    #[error("replica store error: {0}")]
12    Store(#[from] redb::Error),
13    #[error("serialization error: {0}")]
14    Json(#[from] serde_json::Error),
15    #[error("ignore file error: {0}")]
16    Ignore(#[from] ignore::Error),
17    #[error("replica store format {found} is newer than this build supports ({supported})")]
18    FormatTooNew { found: u32, supported: u32 },
19    #[error("replica store belongs to root {stored:?}, not {requested:?}")]
20    RootMismatch { stored: String, requested: String },
21    #[error("replica store is corrupt: {0}")]
22    Corrupt(String),
23    #[error("replica is paused: {0:?}")]
24    Paused(PauseReason),
25    #[error("content of {len} bytes exceeds the {max} byte limit")]
26    TooLarge { len: u64, max: u64 },
27    #[error("received content does not match its hash {expected}")]
28    ContentMismatch { expected: ContentHash },
29    #[cfg(any(test, feature = "test-util"))]
30    #[error("injected fault")]
31    InjectedFault,
32}
33
34/// Result alias for the replication core.
35pub type Result<T> = std::result::Result<T, Error>;
36
37/// Converts redb's per-operation error types into [`Error::Store`].
38pub(crate) trait RedbExt<T> {
39    fn db(self) -> Result<T>;
40}
41
42impl<T, E: Into<redb::Error>> RedbExt<T> for std::result::Result<T, E> {
43    fn db(self) -> Result<T> {
44        self.map_err(|e| Error::Store(e.into()))
45    }
46}