Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: refine error sources #49

Merged
merged 1 commit into from
Sep 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 15 additions & 14 deletions src/builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@

use std::{collections::HashSet, path::PathBuf, str::FromStr, time::Duration};

use bdk_chain::local_chain::MissingGenesisError;
use bdk_wallet::{KeychainKind, Wallet};
use kyoto::{
chain::checkpoints::{
Expand Down Expand Up @@ -164,7 +165,7 @@ impl<'a> LightClientBuilder<'a> {
}

/// Build a light client node and a client to interact with the node.
pub fn build(self) -> Result<(Node, Client<KeychainKind>), Error> {
pub fn build(self) -> Result<(Node, Client<KeychainKind>), BuilderError> {
let network = self.wallet.network();
let mut node_builder = NodeBuilder::new(network);
if let Some(whitelist) = self.peers {
Expand Down Expand Up @@ -232,39 +233,39 @@ impl<'a> LightClientBuilder<'a> {

/// Errors thrown by a client.
#[derive(Debug)]
pub enum Error {
pub enum BuilderError {
/// The `LocalChain` was not initialized with a genesis block.
MissingGenesis(crate::Error),
Chain(MissingGenesisError),
/// The database encountered a fatal error.
Database(DatabaseError),
}

impl std::fmt::Display for Error {
impl std::fmt::Display for BuilderError {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
Error::MissingGenesis(e) => write!(f, "genesis block not found: {e}"),
Error::Database(e) => write!(f, "fatal database error: {e}"),
BuilderError::Chain(e) => write!(f, "genesis block not found: {e}"),
BuilderError::Database(e) => write!(f, "fatal database error: {e}"),
}
}
}

impl std::error::Error for Error {
impl std::error::Error for BuilderError {
fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
match self {
Error::MissingGenesis(e) => Some(e),
Error::Database(e) => Some(e),
BuilderError::Chain(e) => Some(e),
BuilderError::Database(e) => Some(e),
}
}
}

impl From<crate::Error> for Error {
fn from(value: crate::Error) -> Self {
Error::MissingGenesis(value)
impl From<MissingGenesisError> for BuilderError {
fn from(value: MissingGenesisError) -> Self {
BuilderError::Chain(value)
}
}

impl From<DatabaseError> for Error {
impl From<DatabaseError> for BuilderError {
fn from(value: DatabaseError) -> Self {
Error::Database(value)
BuilderError::Database(value)
}
}
64 changes: 11 additions & 53 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,7 @@ where
cp: CheckPoint,
index: &KeychainTxOutIndex<K>,
client: kyoto::Client,
) -> Result<Self, Error> {
) -> Result<Self, MissingGenesisError> {
let (sender, receiver) = client.split();
Ok(Self {
sender,
Expand Down Expand Up @@ -269,25 +269,28 @@ where
}

/// Broadcast a [`Transaction`] with a [`TxBroadcastPolicy`] strategy.
pub async fn broadcast(&self, tx: Transaction, policy: TxBroadcastPolicy) -> Result<(), Error> {
pub async fn broadcast(
&self,
tx: Transaction,
policy: TxBroadcastPolicy,
) -> Result<(), ClientError> {
self.sender
.broadcast_tx(TxBroadcast {
tx,
broadcast_policy: policy,
})
.await
.map_err(Error::from)
}

/// Add more scripts to the node. For example, a user may reveal a Bitcoin address to receive a payment,
/// so this script should be added to the [`Node`].
pub async fn add_script(&self, script: impl Into<ScriptBuf>) -> Result<(), Error> {
self.sender.add_script(script).await.map_err(Error::from)
pub async fn add_script(&self, script: impl Into<ScriptBuf>) -> Result<(), ClientError> {
self.sender.add_script(script).await
}

/// Shutdown the node.
pub async fn shutdown(&self) -> Result<(), Error> {
self.sender.shutdown().await.map_err(Error::from)
pub async fn shutdown(&self) -> Result<(), ClientError> {
self.sender.shutdown().await
}

/// Get a structure to broadcast transactions. Useful for broadcasting transactions and updating concurrently.
Expand Down Expand Up @@ -318,57 +321,12 @@ impl TransactionBroadcaster {
&mut self,
tx: &Transaction,
policy: TxBroadcastPolicy,
) -> Result<(), Error> {
) -> Result<(), ClientError> {
self.sender
.broadcast_tx(TxBroadcast {
tx: tx.clone(),
broadcast_policy: policy,
})
.await
.map_err(Error::from)
}
}

/// Errors thrown by a client.
#[derive(Debug)]
pub enum Error {
/// The channel to receive a message was closed. Likely the node has stopped running.
Sender(ClientError),
/// The [`LocalChain`] provided has no genesis block.
MissingGenesis(MissingGenesisError),
}

impl core::fmt::Display for Error {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Self::Sender(e) => write!(
f,
"the receiving channel has been close. Is the node still running?: {e}"
),
Self::MissingGenesis(e) => {
write!(f, "the local chain provided has no genesis block: {e}")
}
}
}
}

impl std::error::Error for Error {
fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
match self {
Error::Sender(s) => Some(s),
Error::MissingGenesis(m) => Some(m),
}
}
}

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

impl From<ClientError> for Error {
fn from(value: ClientError) -> Self {
Error::Sender(value)
}
}
Loading