Skip to content

Commit

Permalink
feat: add esplora error
Browse files Browse the repository at this point in the history
  • Loading branch information
reez committed Feb 9, 2024
1 parent 50f102b commit 70ef5b5
Showing 1 changed file with 112 additions and 0 deletions.
112 changes: 112 additions & 0 deletions bdk-ffi/src/error.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
use crate::bitcoin::OutPoint;

use bdk::chain::tx_graph::CalculateFeeError as BdkCalculateFeeError;
use bdk_esplora::esplora_client::Error as BdkEsploraError;

use std::fmt;
use std::io;

use bdk::descriptor::DescriptorError;
use bdk::wallet::error::{BuildFeeBumpError, CreateTxError};
Expand Down Expand Up @@ -103,10 +105,75 @@ impl From<BdkCalculateFeeError> for CalculateFeeError {

impl std::error::Error for CalculateFeeError {}

#[derive(Debug)]
pub enum EsploraError {
Ureq(String),
UreqTransport(String),
Http(u16),
Io(io::Error),
NoHeader,
Parsing(io::Error),
BitcoinEncoding(io::Error),
Hex(io::Error),
TransactionNotFound,
HeaderHeightNotFound(u32),
HeaderHashNotFound,
}

impl fmt::Display for EsploraError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
EsploraError::Ureq(message) => write!(f, "Ureq error: {}", message),
EsploraError::UreqTransport(message) => write!(f, "Ureq transport error: {}", message),
EsploraError::Http(code) => write!(f, "HTTP error with status code: {}", code),
EsploraError::Io(err) => write!(f, "IO error: {}", err),
EsploraError::NoHeader => write!(f, "No header found in the response"),
EsploraError::Parsing(err) => write!(f, "Parsing error: {}", err),
EsploraError::BitcoinEncoding(err) => write!(f, "Bitcoin encoding error: {}", err),
EsploraError::Hex(err) => write!(f, "Hex decoding error: {}", err),
EsploraError::TransactionNotFound => write!(f, "Transaction not found"),
EsploraError::HeaderHeightNotFound(height) => {
write!(f, "Header height {} not found", height)
}
EsploraError::HeaderHashNotFound => write!(f, "Header hash not found"),
}
}
}

impl From<BdkEsploraError> for EsploraError {
fn from(error: BdkEsploraError) -> Self {
match error {
BdkEsploraError::Ureq(e) => EsploraError::Ureq(e.to_string()),
BdkEsploraError::UreqTransport(_) => {
EsploraError::UreqTransport("Ureq transport error".to_string())
}
BdkEsploraError::HttpResponse(code) => EsploraError::Http(code),
BdkEsploraError::Io(e) => EsploraError::Io(e),
BdkEsploraError::NoHeader => EsploraError::NoHeader,
BdkEsploraError::Parsing(e) => {
EsploraError::Parsing(io::Error::new(io::ErrorKind::Other, e.to_string()))
}
BdkEsploraError::BitcoinEncoding(e) => {
EsploraError::BitcoinEncoding(io::Error::new(io::ErrorKind::Other, e.to_string()))
}
BdkEsploraError::Hex(e) => {
EsploraError::Hex(io::Error::new(io::ErrorKind::Other, e.to_string()))
}
BdkEsploraError::TransactionNotFound(_) => EsploraError::TransactionNotFound,
BdkEsploraError::HeaderHeightNotFound(height) => {
EsploraError::HeaderHeightNotFound(height)
}
BdkEsploraError::HeaderHashNotFound(_) => EsploraError::HeaderHashNotFound,
}
}
}

#[cfg(test)]
mod test {
use crate::error::EsploraError;
use crate::CalculateFeeError;
use crate::OutPoint;
use std::io;

#[test]
fn test_error_missing_tx_out() {
Expand Down Expand Up @@ -148,4 +215,49 @@ mod test {

assert_eq!(error.to_string(), "Negative fee value: -100");
}

#[test]
fn test_esplora_errors() {
let cases = vec![
(
EsploraError::Ureq("Network error".to_string()),
"Ureq error: Network error",
),
(
EsploraError::UreqTransport("Timeout occurred".to_string()),
"Ureq transport error: Timeout occurred",
),
(EsploraError::Http(404), "HTTP error with status code: 404"),
(
EsploraError::Io(io::Error::new(io::ErrorKind::NotFound, "File not found")),
"IO error: File not found",
),
(EsploraError::NoHeader, "No header found in the response"),
(
EsploraError::Parsing(io::Error::new(io::ErrorKind::InvalidData, "Invalid JSON")),
"Parsing error: Invalid JSON",
),
(
EsploraError::BitcoinEncoding(io::Error::new(
io::ErrorKind::InvalidInput,
"Bad format",
)),
"Bitcoin encoding error: Bad format",
),
(
EsploraError::Hex(io::Error::new(io::ErrorKind::InvalidData, "Invalid hex")),
"Hex decoding error: Invalid hex",
),
(EsploraError::TransactionNotFound, "Transaction not found"),
(
EsploraError::HeaderHeightNotFound(123456),
"Header height 123456 not found",
),
(EsploraError::HeaderHashNotFound, "Header hash not found"),
];

for (error, expected_message) in cases {
assert_eq!(error.to_string(), expected_message);
}
}
}

0 comments on commit 70ef5b5

Please sign in to comment.