car_mirror/
error.rs

1use crate::incremental_verification::BlockState;
2use libipld::Cid;
3use wnfs_common::BlockStoreError;
4
5/// Errors raised from the CAR mirror library
6#[derive(thiserror::Error, Debug)]
7pub enum Error {
8    /// An error raised during receival of blocks, when more than the configured maximum
9    /// bytes are received in a single batch. See the `Config` type.
10    #[error("Expected to receive no more than {receive_maximum} bytes, but got at least {bytes_read}, aborting request.")]
11    TooManyBytes {
12        /// The configured amount of maximum bytes to receive
13        receive_maximum: usize,
14        /// The actual amount of bytes received so far
15        bytes_read: usize,
16    },
17
18    /// An error raised when an individual block exceeded the maximum configured block size
19    #[error("Maximum block size exceeded, maximum configured block size is {max_block_size} bytes, but got {block_bytes} at {cid}")]
20    BlockSizeExceeded {
21        /// The CID of the block that exceeded the maximum
22        cid: Cid,
23        /// The amount of bytes we got for this block up to this point
24        block_bytes: usize,
25        /// The maximum block size from our configuration
26        max_block_size: usize,
27    },
28
29    /// This library only supports a subset of default codecs, including DAG-CBOR, DAG-JSON, DAG-PB and more.
30    /// This is raised if an unknown codec is read from a CID. See the `libipld` library for more information.
31    #[error("Unsupported codec in Cid: {cid}")]
32    UnsupportedCodec {
33        /// The CID with the unsupported codec
34        cid: Cid,
35    },
36
37    /// This library only supports a subset of default hash functions, including SHA-256, SHA-3, BLAKE3 and more.
38    /// This is raised if an unknown hash code is read from a CID. See the `libipld` library for more information.
39    #[error("Unsupported hash code in CID {cid}")]
40    UnsupportedHashCode {
41        /// The CID with the unsupported hash function
42        cid: Cid,
43    },
44
45    /// An error rasied from the blockstore.
46    #[error("BlockStore error: {0}")]
47    BlockStoreError(#[from] BlockStoreError),
48
49    // -------------
50    // Anyhow Errors
51    // -------------
52    /// An error raised when trying to parse a block (e.g. to look for further links)
53    #[error("Error during block parsing: {0}")]
54    ParsingError(anyhow::Error),
55
56    // ----------
57    // Sub-errors
58    // ----------
59    /// Errors related to incremental verification
60    #[error(transparent)]
61    IncrementalVerificationError(#[from] IncrementalVerificationError),
62
63    /// An error rasied when trying to read or write a CAR file.
64    #[error("CAR (de)serialization error: {0}")]
65    CarFileError(#[from] iroh_car::Error),
66}
67
68/// Errors related to incremental verification
69#[derive(thiserror::Error, Debug)]
70pub enum IncrementalVerificationError {
71    /// Raised when we receive a block with a CID that we don't expect.
72    /// We only expect blocks when they're related to the root CID of a DAG.
73    /// So a CID needs to have a path back to the root.
74    #[error("Expected to want block {cid}, but block state is: {block_state:?}")]
75    ExpectedWantedBlock {
76        /// The CID of the block we're currently processing
77        cid: Box<Cid>,
78        /// The block state it has during incremental verification.
79        /// So either we already have it or it's unexpected.
80        block_state: BlockState,
81    },
82
83    /// Raised when the block stored in the CAR file doesn't match its hash.
84    #[error("Digest mismatch in CAR file: expected {cid}, got {actual_cid}")]
85    DigestMismatch {
86        /// The expected CID
87        cid: Box<Cid>,
88        /// The CID it actually hashes to
89        actual_cid: Box<Cid>,
90    },
91}