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 8, 2024
1 parent 50f102b commit 3c10c35
Showing 1 changed file with 73 additions and 0 deletions.
73 changes: 73 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,8 +105,72 @@ 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;

Expand Down Expand Up @@ -148,4 +214,11 @@ mod test {

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

#[test]
fn test_esplora_error_from_ureq_error() {
let error = EsploraError::Ureq("Network error".to_string());

assert_eq!(error.to_string(), "Ureq error: Network error");
}
}

0 comments on commit 3c10c35

Please sign in to comment.