libipld_base/
error.rs

1//! `Ipld` error definitions.
2use multihash::Multihash;
3use thiserror::Error;
4
5/// Result alias.
6pub type Result<T> = core::result::Result<T, BlockError>;
7
8/// Ipld type error.
9#[derive(Debug, Error)]
10pub enum IpldError {
11    /// Expected a boolean.
12    #[error("Expected a boolean.")]
13    NotBool,
14    /// Expected an integer.
15    #[error("Expected an integer.")]
16    NotInteger,
17    /// Expected a float.
18    #[error("Expected a float.")]
19    NotFloat,
20    /// Expected a string.
21    #[error("Expected a string.")]
22    NotString,
23    /// Expected bytes.
24    #[error("Expected bytes.")]
25    NotBytes,
26    /// Expected a list.
27    #[error("Expected a list.")]
28    NotList,
29    /// Expected a map.
30    #[error("Expected a map.")]
31    NotMap,
32    /// Expected a cid.
33    #[error("Expected a cid.")]
34    NotLink,
35    /// Expected a key.
36    #[error("Expected a key.")]
37    NotKey,
38    /// Index not found.
39    #[error("Index not found.")]
40    IndexNotFound,
41    /// Key not found.
42    #[error("Key not found.")]
43    KeyNotFound,
44}
45
46impl From<core::convert::Infallible> for IpldError {
47    fn from(_: core::convert::Infallible) -> Self {
48        unreachable!();
49    }
50}
51
52/// Block error.
53#[derive(Debug, Error)]
54pub enum BlockError {
55    /// Block exceeds MAX_BLOCK_SIZE.
56    #[error("Block size {0} exceeds MAX_BLOCK_SIZE.")]
57    BlockTooLarge(usize),
58    /// Hash does not match the CID.
59    #[error("Hash does not match the CID.")]
60    InvalidHash(Multihash),
61    /// The codec is unsupported.
62    #[error("Unsupported codec {0:?}.")]
63    UnsupportedCodec(cid::Codec),
64    /// The multihash is unsupported.
65    #[error("Unsupported multihash {0:?}.")]
66    UnsupportedMultihash(multihash::Code),
67    /// The codec returned an error.
68    #[error("Codec error: {0}")]
69    CodecError(Box<dyn std::error::Error + Send + Sync>),
70    /// Io error.
71    #[error("{0}")]
72    Io(std::io::Error),
73    /// Cid error.
74    #[error("{0}")]
75    Cid(cid::Error),
76    /// Link error.
77    #[error("Invalid link.")]
78    InvalidLink,
79}
80
81impl From<std::io::Error> for BlockError {
82    fn from(err: std::io::Error) -> Self {
83        Self::Io(err)
84    }
85}
86
87impl From<cid::Error> for BlockError {
88    fn from(err: cid::Error) -> Self {
89        Self::Cid(err)
90    }
91}