Skip to main content

bdk_labels/
error.rs

1use thiserror::Error;
2
3/// Represents all possible errors that can occur within the `bdk-labels` crate.
4#[derive(Debug, Error)]
5pub enum Error {
6    /// An error originating from standard filesystem or stream I/O operations.
7    #[error("I/O error: {0}")]
8    Io(#[from] std::io::Error),
9
10    /// An error related to serializing or deserializing the BIP-329 JSONL format.
11    #[error("JSON parsing error: {0}")]
12    Json(#[from] serde_json::Error),
13
14    /// An error indicating that the provided label data violates the BIP-329 specification.
15    #[error("Invalid BIP329 structure: {0}")]
16    Validation(String),
17
18    /// An opaque error type allowing consumers to bubble up errors from their custom database backends.
19    #[error("Custom Database error: {0}")]
20    Custom(Box<dyn std::error::Error + Send + Sync>),
21}
22
23#[cfg(test)]
24mod tests {
25    use super::*;
26    use std::io;
27
28    #[test]
29    fn test_io_error_conversion() {
30        fn trigger_conversion() -> Result<(), Error> {
31            let io_error = io::Error::other("Forced IO Failure");
32            Err(io_error)?
33        }
34
35        let result = trigger_conversion();
36
37        assert!(matches!(result, Err(Error::Io(_))));
38    }
39
40    #[test]
41    fn test_serde_json_error_conversion() {
42        fn trigger_conversion() -> Result<(), Error> {
43            let json_error =
44                serde_json::from_str::<serde_json::Value>("{ corrupted json string}").unwrap_err();
45            Err(json_error)?
46        }
47
48        let result = trigger_conversion();
49
50        assert!(matches!(result, Err(Error::Json(_))));
51    }
52}