Skip to main content

zcash_client_backend/data_api/chain/
error.rs

1//! Types for chain scanning error handling.
2
3use std::error;
4use std::fmt::{self, Debug, Display};
5
6use crate::scanning::ScanError;
7
8/// Errors related to chain validation and scanning.
9#[derive(Debug)]
10#[non_exhaustive]
11pub enum Error<WalletError, BlockSourceError> {
12    /// An error that was produced by wallet operations in the course of scanning the chain.
13    Wallet(WalletError),
14
15    /// An error that was produced by the underlying block data store in the process of validation
16    /// or scanning.
17    BlockSource(BlockSourceError),
18
19    /// A block that was received violated rules related to chain continuity or contained note
20    /// commitments that could not be reconciled with the note commitment tree(s) maintained by the
21    /// wallet.
22    Scan(ScanError),
23}
24
25impl<WE: fmt::Display, BE: fmt::Display> fmt::Display for Error<WE, BE> {
26    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
27        match &self {
28            Error::Wallet(e) => {
29                write!(
30                    f,
31                    "The underlying datasource produced the following error: {e}"
32                )
33            }
34            Error::BlockSource(e) => {
35                write!(
36                    f,
37                    "The underlying block store produced the following error: {e}"
38                )
39            }
40            Error::Scan(e) => {
41                write!(f, "Scanning produced the following error: {e}")
42            }
43        }
44    }
45}
46
47impl<WE, BE> error::Error for Error<WE, BE>
48where
49    WE: Debug + Display + error::Error + 'static,
50    BE: Debug + Display + error::Error + 'static,
51{
52    fn source(&self) -> Option<&(dyn error::Error + 'static)> {
53        match &self {
54            Error::Wallet(e) => Some(e),
55            Error::BlockSource(e) => Some(e),
56            _ => None,
57        }
58    }
59}
60
61impl<WE, BSE> From<ScanError> for Error<WE, BSE> {
62    fn from(e: ScanError) -> Self {
63        Error::Scan(e)
64    }
65}