1use thiserror::Error;
2
3#[derive(Debug, Error)]
5pub enum Error {
6 #[error("I/O error: {0}")]
8 Io(#[from] std::io::Error),
9
10 #[error("JSON parsing error: {0}")]
12 Json(#[from] serde_json::Error),
13
14 #[error("Invalid BIP329 structure: {0}")]
16 Validation(String),
17
18 #[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}