Skip to content

Commit

Permalink
chore: convert subxt errors to a human readable format
Browse files Browse the repository at this point in the history
Signed-off-by: Gregory Hill <[email protected]>
  • Loading branch information
gregdhill committed Aug 21, 2023
1 parent 82e085a commit d3be02c
Show file tree
Hide file tree
Showing 12 changed files with 50 additions and 97 deletions.
12 changes: 12 additions & 0 deletions runtime/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,18 @@ impl From<module_bitcoin::Error> for Error {
}

impl Error {
pub fn to_human(self) -> String {
match self {
Error::SubxtRuntimeError(SubxtError::Runtime(DispatchError::Module(module_error))) => {
match module_error.as_root_error::<crate::metadata::Error>() {
Ok(root_error) => format!("{:?}", root_error),
Err(_) => format!("Unknown error: {:?}", module_error.raw()),
}
}
err => err.to_string(),
}
}

pub fn is_any_module_err(&self) -> bool {
matches!(
self,
Expand Down
1 change: 0 additions & 1 deletion runtime/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,6 @@ pub const DISABLE_DIFFICULTY_CHECK: &str = "DisableDifficultyCheck";
),
)
)]

pub mod metadata {}

#[derive(Debug, Eq, PartialEq, Ord, PartialOrd, Default, Clone, Decode, Encode)]
Expand Down
12 changes: 6 additions & 6 deletions runtime/src/rpc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -392,14 +392,14 @@ impl InterBtcParachain {
///
/// # Arguments
/// * `on_error` - callback for decoding errors, is not allowed to take too long
pub async fn on_event_error<E: Fn(SubxtError)>(&self, on_error: E) -> Result<(), Error> {
pub async fn on_event_error<E: Fn(Error)>(&self, on_error: E) -> Result<(), Error> {
let mut sub = self.subscribe_events().await?;

loop {
match sub.next().await {
Some(Err(err)) => on_error(err), // report error
Some(Ok(_)) => {} // do nothing
None => break Ok(()), // end of stream
Some(Err(err)) => on_error(err.into()), // report error
Some(Ok(_)) => {} // do nothing
None => break Ok(()), // end of stream
}
}
}
Expand All @@ -420,7 +420,7 @@ impl InterBtcParachain {
T: StaticEvent + core::fmt::Debug,
F: FnMut(T) -> R,
R: Future<Output = ()>,
E: Fn(SubxtError),
E: Fn(Error),
{
let mut sub = self.subscribe_events().await?;
let (tx, mut rx) = futures::channel::mpsc::channel::<T>(32);
Expand All @@ -442,7 +442,7 @@ impl InterBtcParachain {
break;
}
}
Err(err) => on_error(err),
Err(err) => on_error(err.into()),
}
}
}
Expand Down
4 changes: 2 additions & 2 deletions runtime/src/types.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use crate::{
metadata,
utils::{account_id::AccountId32, signer::PairSigner},
Config, InterBtcRuntime, RuntimeCurrencyInfo,
Config, InterBtcRuntime, RuntimeCurrencyInfo, SS58_PREFIX,
};
pub use currency_id::CurrencyIdExt;
pub use h256_le::RichH256Le;
Expand Down Expand Up @@ -211,7 +211,7 @@ mod account_id {
use sp_core::crypto::Ss58Codec;
impl PrettyPrint for AccountId {
fn pretty_print(&self) -> String {
self.0.to_ss58check()
self.0.to_ss58check_with_version(SS58_PREFIX.into())
}
}
}
Expand Down
6 changes: 3 additions & 3 deletions vault/src/connection_manager.rs
Original file line number Diff line number Diff line change
Expand Up @@ -136,12 +136,12 @@ impl<Config: Clone + Send + 'static, F: Fn()> ConnectionManager<Config, F> {
);

match service.start().await {
Err(err @ backoff::Error::Permanent(_)) => {
Err(backoff::Error::Permanent(err)) => {
tracing::warn!("Disconnected: {}", err);
return Err(err.into());
}
Err(err) => {
tracing::warn!("Disconnected: {}", err);
Err(backoff::Error::Transient(err)) => {
tracing::warn!("Disconnected: {}", err.to_human());
}
_ => {
tracing::warn!("Disconnected");
Expand Down
9 changes: 9 additions & 0 deletions vault/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,15 @@ pub enum Error {
IoError(#[from] IoError),
}

impl Error {
pub fn to_human(self) -> String {
match self {
Self::RuntimeError(runtime_error) => runtime_error.to_human(),
err => err.to_string(),
}
}
}

impl From<backoff::Error<Error>> for Error {
fn from(err: backoff::Error<Error>) -> Self {
match err {
Expand Down
10 changes: 5 additions & 5 deletions vault/src/issue.rs
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ pub async fn process_issue_requests(
random_delay.clone(),
)
.map_err(|e| {
tracing::warn!("Failed to execute issue request: {}", e.to_string());
tracing::warn!("Failed to execute issue request: {}", e.to_human());
}),
),
Err(err) => return Err(err.into()),
Expand Down Expand Up @@ -382,7 +382,7 @@ pub async fn listen_for_issue_requests(
let _ = publish_expected_bitcoin_balance(&vault, btc_parachain.clone()).await;

if let Err(e) = add_new_deposit_key(&vault.btc_rpc, *event.issue_id, event.vault_public_key).await {
tracing::error!("Failed to add new deposit key #{}: {}", *event.issue_id, e.to_string());
tracing::error!("Failed to add new deposit key #{}: {}", *event.issue_id, e.to_human());
}
}

Expand All @@ -393,7 +393,7 @@ pub async fn listen_for_issue_requests(
);
issue_set.insert(*event.issue_id, *event.vault_address).await;
},
|error| tracing::error!("Error reading request issue event: {}", error.to_string()),
|error| tracing::error!("Error reading request issue event: {}", error.to_human()),
)
.await?;
Ok(())
Expand Down Expand Up @@ -428,7 +428,7 @@ pub async fn listen_for_issue_executes(
tracing::trace!("issue #{} executed, no longer watching", *event.issue_id);
issue_set.remove(&event.issue_id).await;
},
|error| tracing::error!("Error reading execute issue event: {}", error.to_string()),
|error| tracing::error!("Error reading execute issue event: {}", error.to_human()),
)
.await?;
Ok(())
Expand All @@ -451,7 +451,7 @@ pub async fn listen_for_issue_cancels(
tracing::trace!("issue #{} cancelled, no longer watching", *event.issue_id);
issue_set.remove(&event.issue_id).await;
},
|error| tracing::error!("Error reading cancel issue event: {}", error.to_string()),
|error| tracing::error!("Error reading cancel issue event: {}", error.to_human()),
)
.await?;
Ok(())
Expand Down
2 changes: 1 addition & 1 deletion vault/src/metrics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -580,7 +580,7 @@ pub async fn monitor_bridge_metrics(
}
}
},
|error| tracing::error!("Error reading SetExchangeRate event: {}", error.to_string()),
|error| tracing::error!("Error reading SetExchangeRate event: {}", error.to_human()),
)
.await?;
Ok(())
Expand Down
4 changes: 2 additions & 2 deletions vault/src/redeem.rs
Original file line number Diff line number Diff line change
Expand Up @@ -64,12 +64,12 @@ pub async fn listen_for_redeem_requests(
Err(e) => tracing::error!(
"Failed to process redeem request #{}: {}",
*event.redeem_id,
e.to_string()
e.to_human()
),
}
});
},
|error| tracing::error!("Error reading redeem event: {}", error.to_string()),
|error| tracing::error!("Error reading redeem event: {}", error.to_human()),
)
.await?;
Ok(())
Expand Down
68 changes: 0 additions & 68 deletions vault/src/refund.rs

This file was deleted.

10 changes: 5 additions & 5 deletions vault/src/replace.rs
Original file line number Diff line number Diff line change
Expand Up @@ -73,12 +73,12 @@ pub async fn listen_for_accept_replace(
Err(e) => tracing::error!(
"Failed to process accept replace request #{}: {}",
*event.replace_id,
e.to_string()
e.to_human()
),
}
});
},
|error| tracing::error!("Error reading accept_replace_event: {}", error.to_string()),
|error| tracing::error!("Error reading accept_replace_event: {}", error.to_human()),
)
.await?;
Ok(())
Expand Down Expand Up @@ -133,13 +133,13 @@ pub async fn listen_for_replace_requests(
"[{}] Failed to accept replace request from {}: {}",
vault_id.pretty_print(),
event.old_vault_id.pretty_print(),
e.to_string()
e.to_human()
),
}
}
}
},
|error| tracing::error!("Error reading replace event: {}", error.to_string()),
|error| tracing::error!("Error reading replace event: {}", error.to_human()),
)
.await?;
Ok(())
Expand Down Expand Up @@ -201,7 +201,7 @@ pub async fn listen_for_execute_replace(
let _ = event_channel.clone().send(Event::Executed(*event.replace_id)).await;
}
},
|error| tracing::error!("Error reading redeem event: {}", error.to_string()),
|error| tracing::error!("Error reading redeem event: {}", error.to_human()),
)
.await?;
Ok(())
Expand Down
9 changes: 5 additions & 4 deletions vault/src/system.rs
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@ async fn active_block_listener(
let _ = issue_tx.clone().send(Event::ParachainBlock(event.block_number)).await;
let _ = replace_tx.clone().send(Event::ParachainBlock(event.block_number)).await;
},
|err| tracing::error!("Error (UpdateActiveBlockEvent): {}", err.to_string()),
|err| tracing::error!("Error (UpdateActiveBlockEvent): {}", err.to_human()),
)
.await?;
Ok(())
Expand All @@ -159,7 +159,7 @@ async fn relay_block_listener(
let _ = issue_tx.clone().send(Event::BitcoinBlock(event.block_height)).await;
let _ = replace_tx.clone().send(Event::BitcoinBlock(event.block_height)).await;
},
|err| tracing::error!("Error (StoreMainChainHeaderEvent): {}", err.to_string()),
|err| tracing::error!("Error (StoreMainChainHeaderEvent): {}", err.to_human()),
)
.await?;
Ok(())
Expand Down Expand Up @@ -311,6 +311,7 @@ impl VaultIdManager {
// issue keys should be imported separately but we need to iterate
// through currency specific wallets to get change addresses
for address in btc_rpc.list_addresses()? {
tracing::info!("Found {:?}", address);
// get private key from currency specific wallet
let private_key = btc_rpc.dump_private_key(&address)?;
// import key into main wallet
Expand Down Expand Up @@ -367,7 +368,7 @@ impl VaultIdManager {
let _ = self.add_vault_id(vault_id).await;
}
},
|err| tracing::error!("Error (RegisterVaultEvent): {}", err.to_string()),
|err| tracing::error!("Error (RegisterVaultEvent): {}", err.to_human()),
)
.await?)
}
Expand Down Expand Up @@ -619,7 +620,7 @@ impl VaultService {
// NOTE: this will block if subsequent errors do not trigger shutdown
let err_listener = wait_or_shutdown(self.shutdown.clone(), async move {
err_provider
.on_event_error(|e| tracing::debug!("Received error event: {}", e))
.on_event_error(|e| tracing::debug!("Received error event: {}", e.to_human()))
.await?;
Ok::<_, Error>(())
});
Expand Down

0 comments on commit d3be02c

Please sign in to comment.