decds_lib/
errors.rs

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