Skip to content

Commit

Permalink
Merge pull request #21 from rajarshimaitra/fix/error-handling
Browse files Browse the repository at this point in the history
Updated Error Handling.
  • Loading branch information
i5hi authored Feb 14, 2024
2 parents dd96beb + 9d24952 commit cb53d63
Show file tree
Hide file tree
Showing 12 changed files with 651 additions and 1,113 deletions.
141 changes: 141 additions & 0 deletions src/error.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,141 @@
/// The Global Error enum. Encodes all possible internal library errors
#[derive(Debug)]
pub enum Error {
Electrum(electrum_client::Error),
Hex(String),
Protocol(String),
Key(bitcoin::key::Error),
Address(String),
Sighash(bitcoin::sighash::Error),
Secp(bitcoin::secp256k1::Error),
HTTP(ureq::Error),
JSON(serde_json::Error),
IO(std::io::Error),
Bolt11(lightning_invoice::ParseOrSemanticError),
LiquidEncode(elements::encode::Error),
Blind(String),
ConfidentialTx(elements::ConfidentialTxOutError),
BIP32(bitcoin::bip32::Error),
BIP39(bip39::Error),
Hash(bitcoin::hashes::FromSliceError),
}

impl From<electrum_client::Error> for Error {
fn from(value: electrum_client::Error) -> Self {
Self::Electrum(value)
}
}

impl From<bitcoin::hex::HexToBytesError> for Error {
fn from(value: bitcoin::hex::HexToBytesError) -> Self {
Self::Hex(value.to_string())
}
}

impl From<bitcoin::key::Error> for Error {
fn from(value: bitcoin::key::Error) -> Self {
Self::Key(value)
}
}

impl From<bitcoin::hex::HexToArrayError> for Error {
fn from(value: bitcoin::hex::HexToArrayError) -> Self {
Self::Hex(value.to_string())
}
}

impl From<bitcoin::address::ParseError> for Error {
fn from(value: bitcoin::address::ParseError) -> Self {
Self::Address(value.to_string())
}
}

impl From<elements::address::AddressError> for Error {
fn from(value: elements::address::AddressError) -> Self {
Self::Address(value.to_string())
}
}

impl From<bitcoin::sighash::Error> for Error {
fn from(value: bitcoin::sighash::Error) -> Self {
Self::Sighash(value)
}
}

impl From<bitcoin::secp256k1::Error> for Error {
fn from(value: bitcoin::secp256k1::Error) -> Self {
Self::Secp(value)
}
}

impl From<ureq::Error> for Error {
fn from(value: ureq::Error) -> Self {
Self::HTTP(value)
}
}

impl From<serde_json::Error> for Error {
fn from(value: serde_json::Error) -> Self {
Self::JSON(value)
}
}

impl From<std::io::Error> for Error {
fn from(value: std::io::Error) -> Self {
Self::IO(value)
}
}

impl From<lightning_invoice::ParseOrSemanticError> for Error {
fn from(value: lightning_invoice::ParseOrSemanticError) -> Self {
Self::Bolt11(value)
}
}

impl From<elements::hex::Error> for Error {
fn from(value: elements::hex::Error) -> Self {
Self::Hex(value.to_string())
}
}

impl From<elements::encode::Error> for Error {
fn from(value: elements::encode::Error) -> Self {
Self::LiquidEncode(value)
}
}

impl From<elements::BlindError> for Error {
fn from(value: elements::BlindError) -> Self {
Self::Blind(value.to_string())
}
}

impl From<elements::UnblindError> for Error {
fn from(value: elements::UnblindError) -> Self {
Self::Blind(value.to_string())
}
}

impl From<elements::ConfidentialTxOutError> for Error {
fn from(value: elements::ConfidentialTxOutError) -> Self {
Self::ConfidentialTx(value)
}
}

impl From<bitcoin::bip32::Error> for Error {
fn from(value: bitcoin::bip32::Error) -> Self {
Self::BIP32(value)
}
}

impl From<bitcoin::hashes::FromSliceError> for Error {
fn from(value: bitcoin::hashes::FromSliceError) -> Self {
Self::Hash(value)
}
}

impl From<bip39::Error> for Error {
fn from(value: bip39::Error) -> Self {
Self::BIP39(value)
}
}
4 changes: 4 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@
//! Refer to tests/ folder for usage
//! THIS LIBRARY IS IN EARLY ALPHA. TEST AND REVIEW BEFORE USING IN PRODUCTION.

#![allow(unused)]
/// Error Module
mod error;
/// Blockchain Network module. Currently only contains electrum interface.
pub mod network;
/// core swap logic
pub mod swaps;
Expand Down
11 changes: 4 additions & 7 deletions src/network/electrum.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// use electrum_client::raw_client::RawClient;

use crate::util::error::{ErrorKind, S5Error};
use crate::error::Error;

use super::Chain;

Expand All @@ -16,7 +16,7 @@ enum ElectrumUrl {
}

impl ElectrumUrl {
pub fn build_client(&self, timeout: u8) -> Result<electrum_client::Client, S5Error> {
pub fn build_client(&self, timeout: u8) -> Result<electrum_client::Client, Error> {
let builder = electrum_client::ConfigBuilder::new();
let builder = builder.timeout(Some(timeout));
let (url, builder) = match self {
Expand All @@ -25,10 +25,7 @@ impl ElectrumUrl {
}
ElectrumUrl::Plaintext(url) => (format!("tcp://{}", url), builder),
};
match electrum_client::Client::from_config(&url, builder.build()) {
Ok(result) => Ok(result),
Err(e) => Err(S5Error::new(ErrorKind::Network, &e.to_string())),
}
Ok(electrum_client::Client::from_config(&url, builder.build())?)
}
}

Expand Down Expand Up @@ -81,7 +78,7 @@ impl ElectrumConfig {
self.network.clone()
}
/// Builds an electrum_client::Client which can be used to make calls to electrum api
pub fn build_client(&self) -> Result<electrum_client::Client, S5Error> {
pub fn build_client(&self) -> Result<electrum_client::Client, Error> {
self.url.clone().build_client(self.timeout)
}
}
Expand Down
Loading

0 comments on commit cb53d63

Please sign in to comment.