Skip to content

Commit e4ce824

Browse files
committed
Add error logging when mining
1 parent c1de12a commit e4ce824

File tree

2 files changed

+37
-2
lines changed

2 files changed

+37
-2
lines changed

app/app.rs

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -369,12 +369,20 @@ impl App {
369369

370370
tracing::debug!(%bmm_txid, "mine: confirming BMM...");
371371
if let Some((main_hash, header, body)) =
372-
miner_write.confirm_bmm().await?
372+
miner_write.confirm_bmm().await.inspect_err(|err| {
373+
tracing::error!("{:#}", thunder::util::ErrorChain::new(err))
374+
})?
373375
{
374376
tracing::debug!(
375377
%main_hash, side_hash = %header.hash(), "mine: confirmed BMM, submitting block",
376378
);
377-
match self.node.submit_block(main_hash, &header, &body).await? {
379+
match self
380+
.node
381+
.submit_block(main_hash, &header, &body)
382+
.await
383+
.inspect_err(|err| {
384+
tracing::error!("{:#}", thunder::util::ErrorChain::new(err))
385+
})? {
378386
true => {
379387
tracing::debug!(
380388
%main_hash, "mine: BMM accepted as new tip",

lib/util.rs

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,3 +111,30 @@ pub mod watchable {
111111
}
112112
}
113113
pub use watchable::Watchable;
114+
115+
/// Display an error with causes.
116+
/// This is useful for displaying errors without converting to
117+
/// `miette::Report` or `anyhow::Error` first
118+
pub struct ErrorChain<'a>(&'a (dyn std::error::Error));
119+
120+
impl<'a> ErrorChain<'a> {
121+
pub fn new<E>(err: &'a E) -> Self
122+
where
123+
E: std::error::Error,
124+
{
125+
Self(err)
126+
}
127+
}
128+
129+
impl std::fmt::Display for ErrorChain<'_> {
130+
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
131+
std::fmt::Display::fmt(&self.0, f)?;
132+
let mut source: Option<&dyn std::error::Error> = self.0.source();
133+
while let Some(cause) = source {
134+
std::fmt::Display::fmt(": ", f)?;
135+
std::fmt::Display::fmt(cause, f)?;
136+
source = cause.source();
137+
}
138+
Ok(())
139+
}
140+
}

0 commit comments

Comments
 (0)