decds_lib/
errors.rs

1use crate::{chunkset::ChunkSet, consts};
2
3/// Errors encountered by `decds` during blob building and repairing.
4#[derive(PartialEq)]
5pub enum DecdsError {
6    /// Returned when trying to create a blob with empty data.
7    EmptyDataForBlob,
8    /// Returned when a byte range operation has an invalid start bound.
9    InvalidStartBound,
10    /// Returned when a byte range operation has an invalid end bound. Contains the invalid end value.
11    InvalidEndBound(usize),
12
13    /// Returned when `BlobHeader` serialization fails. Contains the error message from the underlying serialization library.
14    BlobHeaderSerializationFailed(String),
15    /// Returned when `BlobHeader` deserialization fails. Contains the error message from the underlying deserialization library.
16    BlobHeaderDeserializationFailed(String),
17
18    /// Returned when `ProofCarryingChunk` serialization fails. Contains the error message from the underlying serialization library.
19    ProofCarryingChunkSerializationFailed(String),
20    /// Returned when `ProofCarryingChunk` deserialization fails. Contains the error message from the underlying deserialization library.
21    ProofCarryingChunkDeserializationFailed(String),
22
23    /// Returned when attempting to add a chunk to a `RepairingChunkSet` that is already ready for repair. Contains the chunkset ID.
24    ChunksetReadyToRepair(usize),
25    /// Returned when attempting to repair a `RepairingChunkSet` that is not yet ready. Contains the chunkset ID.
26    ChunksetNotYetReadyToRepair(usize),
27    /// Returned when attempting to add a chunk to a `RepairingChunkSet` that has already been repaired. Contains the chunkset ID.
28    ChunksetAlreadyRepaired(usize),
29    /// Returned when `RepairingChunkSet` fails to repair its data. Contains the chunkset ID and an error message.
30    ChunksetRepairingFailed(usize, String),
31
32    /// Returned when an invalid erasure-coded share ID is provided. Contains the invalid share ID.
33    InvalidErasureCodedShareId(usize),
34    /// Returned when an invalid chunkset ID is provided. Contains the invalid chunkset ID and the total number of chunksets.
35    InvalidChunksetId(usize, usize),
36    /// Returned when creating a `ChunkSet` with data of an invalid size. Contains the provided size.
37    InvalidChunksetSize(usize),
38    /// Returned when a chunk contains metadata (e.g., chunkset ID) that does not match the expected context. Contains the chunkset ID.
39    InvalidChunkMetadata(usize),
40    /// Returned when a `ProofCarryingChunk` fails its Merkle proof validation. Contains the chunkset ID.
41    InvalidProofInChunk(usize),
42    /// Returned when decoding a chunk fails during the repair process. Contains the chunkset ID and an error message.
43    ChunkDecodingFailed(usize, String),
44
45    /// Returned when attempting to build a Merkle tree with no leaf nodes.
46    NoLeafNodesToBuildMerkleTreeOn,
47    /// Returned when a Merkle tree operation specifies an invalid leaf node index. Contains the invalid index and the total number of leaves.
48    InvalidLeafNodeIndex(usize, usize),
49}
50
51impl std::fmt::Display for DecdsError {
52    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
53        match self {
54            DecdsError::EmptyDataForBlob => write!(f, "empty data for blob"),
55            DecdsError::InvalidStartBound => write!(f, "invalid start bound"),
56            DecdsError::InvalidEndBound(end) => write!(f, "invalid end bound: {}", end),
57
58            DecdsError::BlobHeaderSerializationFailed(err) => write!(f, "failed to serialize blob header: {}", err),
59            DecdsError::BlobHeaderDeserializationFailed(err) => write!(f, "failed to deserialize blob header: {}", err),
60
61            DecdsError::ProofCarryingChunkSerializationFailed(err) => write!(f, "failed to serialize proof carrying chunk: {}", err),
62            DecdsError::ProofCarryingChunkDeserializationFailed(err) => write!(f, "failed to deserialize proof carrying chunk: {}", err),
63
64            DecdsError::ChunksetReadyToRepair(id) => write!(f, "chunkset {} is ready to repair", id),
65            DecdsError::ChunksetNotYetReadyToRepair(id) => write!(f, "chunkset {} is not ready to repair", id),
66            DecdsError::ChunksetAlreadyRepaired(id) => write!(f, "chunkset {} is already repaired", id),
67            DecdsError::ChunksetRepairingFailed(id, err) => write!(f, "chunkset {} repairing failed: {}", id, err),
68
69            DecdsError::InvalidErasureCodedShareId(id) => write!(
70                f,
71                "invalid erasure coded share id: {} (num_shares: {})",
72                id,
73                consts::DECDS_NUM_ERASURE_CODED_SHARES
74            ),
75            DecdsError::InvalidChunksetId(id, num_chunksets) => write!(f, "invalid chunkset id: {} (num_chunksets: {})", id, num_chunksets),
76            DecdsError::InvalidChunksetSize(size) => write!(f, "invalid chunkset size: {}B, expected: {}B", size, ChunkSet::BYTE_LENGTH),
77            DecdsError::InvalidChunkMetadata(chunkset_id) => write!(f, "invalid chunk for chunkset {}", chunkset_id),
78            DecdsError::InvalidProofInChunk(chunkset_id) => write!(f, "invalid proof carrying chunk for chunkset {}", chunkset_id),
79            DecdsError::ChunkDecodingFailed(chunkset_id, err) => write!(f, "decoding chunk for chunkset {} failed: {}", chunkset_id, err),
80
81            DecdsError::NoLeafNodesToBuildMerkleTreeOn => write!(f, "no leaf nodes to build merkle tree on"),
82            DecdsError::InvalidLeafNodeIndex(leaf_index, num_leaves) => write!(f, "invalid leaf node index: {} (num_leaves: {})", leaf_index, num_leaves),
83        }
84    }
85}
86
87impl std::fmt::Debug for DecdsError {
88    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
89        write!(f, "{}", self)
90    }
91}
92
93impl std::error::Error for DecdsError {}