kyoto/db/
error.rs

1/// Errors when initializing a SQL-based backend.
2#[cfg(feature = "rusqlite")]
3#[derive(Debug)]
4pub enum SqlInitializationError {
5    /// A file or directory could not be opened or created.
6    IO(std::io::Error),
7    /// An error occured performing a SQL operation.
8    SQL(rusqlite::Error),
9}
10
11#[cfg(feature = "rusqlite")]
12impl core::fmt::Display for SqlInitializationError {
13    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
14        match self {
15            SqlInitializationError::IO(e) => {
16                write!(f, "a file or directory could not be opened or created: {e}")
17            }
18            SqlInitializationError::SQL(e) => {
19                write!(f, "reading or writing from the database failed: {e}")
20            }
21        }
22    }
23}
24
25#[cfg(feature = "rusqlite")]
26impl std::error::Error for SqlInitializationError {
27    fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
28        match self {
29            SqlInitializationError::IO(error) => Some(error),
30            SqlInitializationError::SQL(error) => Some(error),
31        }
32    }
33}
34
35#[cfg(feature = "rusqlite")]
36impl From<rusqlite::Error> for SqlInitializationError {
37    fn from(value: rusqlite::Error) -> Self {
38        Self::SQL(value)
39    }
40}
41
42#[cfg(feature = "rusqlite")]
43impl From<std::io::Error> for SqlInitializationError {
44    fn from(value: std::io::Error) -> Self {
45        Self::IO(value)
46    }
47}
48
49/// Errors while reading or writing to and from a SQL-based peer backend.
50#[cfg(feature = "rusqlite")]
51#[derive(Debug)]
52pub enum SqlPeerStoreError {
53    /// A consensus critical data structure is malformed.
54    Deserialize(bitcoin::consensus::encode::Error),
55    /// There are no known peers in the database.
56    Empty,
57    /// An error occured performing a SQL operation.
58    SQL(rusqlite::Error),
59}
60
61#[cfg(feature = "rusqlite")]
62impl core::fmt::Display for SqlPeerStoreError {
63    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
64        match self {
65            SqlPeerStoreError::Deserialize(e) => {
66                write!(
67                    f,
68                    "a byte array could not be deserialized into a known datatype: {e}"
69                )
70            }
71            Self::Empty => {
72                write!(f, "there are no known peers in the database.")
73            }
74            SqlPeerStoreError::SQL(e) => {
75                write!(f, "reading or writing from the database failed: {e}")
76            }
77        }
78    }
79}
80
81#[cfg(feature = "rusqlite")]
82impl std::error::Error for SqlPeerStoreError {
83    fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
84        match self {
85            SqlPeerStoreError::Deserialize(error) => Some(error),
86            SqlPeerStoreError::Empty => None,
87            SqlPeerStoreError::SQL(error) => Some(error),
88        }
89    }
90}
91
92#[cfg(feature = "rusqlite")]
93impl From<rusqlite::Error> for SqlPeerStoreError {
94    fn from(value: rusqlite::Error) -> Self {
95        Self::SQL(value)
96    }
97}
98
99#[cfg(feature = "rusqlite")]
100impl From<bitcoin::consensus::encode::Error> for SqlPeerStoreError {
101    fn from(value: bitcoin::consensus::encode::Error) -> Self {
102        Self::Deserialize(value)
103    }
104}
105
106/// Errors while reading or writing to and from a SQL-based block header backend.
107#[cfg(feature = "rusqlite")]
108#[derive(Debug)]
109pub enum SqlHeaderStoreError {
110    /// The headers do not link together.
111    Corruption,
112    /// Consensus deserialization failed.
113    Deserialize(bitcoin::consensus::encode::Error),
114    /// An error occured performing a SQL operation.
115    SQL(rusqlite::Error),
116}
117
118#[cfg(feature = "rusqlite")]
119impl core::fmt::Display for SqlHeaderStoreError {
120    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
121        match self {
122            SqlHeaderStoreError::SQL(e) => {
123                write!(f, "reading or writing from the database failed: {e}")
124            }
125            SqlHeaderStoreError::Deserialize(e) => {
126                write!(f, "consensus decoding failed {e}")
127            }
128            SqlHeaderStoreError::Corruption => {
129                write!(f, "a consensus critical data structure is malformed.")
130            }
131        }
132    }
133}
134
135#[cfg(feature = "rusqlite")]
136impl std::error::Error for SqlHeaderStoreError {
137    fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
138        match self {
139            SqlHeaderStoreError::Corruption => None,
140            SqlHeaderStoreError::SQL(error) => Some(error),
141            SqlHeaderStoreError::Deserialize(error) => Some(error),
142        }
143    }
144}
145
146#[cfg(feature = "rusqlite")]
147impl From<rusqlite::Error> for SqlHeaderStoreError {
148    fn from(value: rusqlite::Error) -> Self {
149        Self::SQL(value)
150    }
151}
152
153#[cfg(feature = "rusqlite")]
154impl From<bitcoin::consensus::encode::Error> for SqlHeaderStoreError {
155    fn from(value: bitcoin::consensus::encode::Error) -> Self {
156        Self::Deserialize(value)
157    }
158}