From 2c9ab4f6ebb18903c3a6d930f48dc555a98dde22 Mon Sep 17 00:00:00 2001 From: Thomas Eizinger Date: Thu, 18 Mar 2021 17:48:54 +1100 Subject: [PATCH] Improve code structure and error messages for running swaps The quote message was repeated and we should set the overall failure into a context to know what went wrong. --- swap/src/bin/swap.rs | 18 +++++++++--------- swap/src/protocol/bob/event_loop.rs | 2 +- 2 files changed, 10 insertions(+), 10 deletions(-) diff --git a/swap/src/bin/swap.rs b/swap/src/bin/swap.rs index 2455753c..8af25443 100644 --- a/swap/src/bin/swap.rs +++ b/swap/src/bin/swap.rs @@ -111,7 +111,7 @@ async fn main() -> Result<()> { alice_addr, bitcoin_wallet.clone(), )?; - let handle = tokio::spawn(event_loop.run()); + let event_loop = tokio::spawn(event_loop.run()); let send_bitcoin = determine_btc_to_swap( event_loop_handle.request_quote(), @@ -142,13 +142,14 @@ async fn main() -> Result<()> { .with_init_params(send_bitcoin) .build()?; - let swap = bob::run(swap); tokio::select! { - event_loop_result = handle => { - event_loop_result??; + result = event_loop => { + result + .context("EventLoop panicked")? + .context("EventLoop failed")?; }, - swap_result = swap => { - swap_result?; + result = bob::run(swap) => { + result.context("Failed to complete swap")?; } } } @@ -207,12 +208,11 @@ async fn main() -> Result<()> { ) .build()?; - let swap = bob::run(swap); tokio::select! { event_loop_result = handle => { event_loop_result??; }, - swap_result = swap => { + swap_result = bob::run(swap) => { swap_result?; } } @@ -314,7 +314,7 @@ async fn determine_btc_to_swap( ) -> Result { debug!("Requesting quote"); - let bid_quote = request_quote.await.context("Failed to request quote")?; + let bid_quote = request_quote.await?; info!("Received quote: 1 XMR ~ {}", bid_quote.price); diff --git a/swap/src/protocol/bob/event_loop.rs b/swap/src/protocol/bob/event_loop.rs index a279ddab..47c7c195 100644 --- a/swap/src/protocol/bob/event_loop.rs +++ b/swap/src/protocol/bob/event_loop.rs @@ -223,7 +223,7 @@ impl EventLoop { } OutEvent::ResponseSent => {} OutEvent::CommunicationError(err) => { - bail!("Communication error: {:#}", err) + bail!(err.context("Communication error")) } } },