Return original error in case downcasting to JsonRpcError fails

This commit is contained in:
Daniel Karzel 2020-12-18 13:53:24 +11:00 committed by rishflab
parent 298fbbe6cb
commit cedc56a160
2 changed files with 14 additions and 12 deletions

View file

@ -18,7 +18,7 @@ use crate::{
storage::Database, storage::Database,
SwapAmounts, TRANSACTION_ALREADY_IN_BLOCKCHAIN_ERROR_CODE, SwapAmounts, TRANSACTION_ALREADY_IN_BLOCKCHAIN_ERROR_CODE,
}; };
use anyhow::{anyhow, bail, Result}; use anyhow::{bail, Result};
use async_recursion::async_recursion; use async_recursion::async_recursion;
use bitcoin_harness::bitcoind_rpc::jsonrpc_client::JsonRpcError; use bitcoin_harness::bitcoind_rpc::jsonrpc_client::JsonRpcError;
use futures::{ use futures::{
@ -475,11 +475,12 @@ pub async fn run_until(
} }
AliceState::T1Expired { state3 } => { AliceState::T1Expired { state3 } => {
if let Err(error) = state3.submit_tx_cancel(bitcoin_wallet.as_ref()).await { if let Err(error) = state3.submit_tx_cancel(bitcoin_wallet.as_ref()).await {
let json_rpc_err = error if let Some(json_rpc_err) = error.downcast_ref::<JsonRpcError>() {
.downcast_ref::<JsonRpcError>() if json_rpc_err.code == TRANSACTION_ALREADY_IN_BLOCKCHAIN_ERROR_CODE {
.ok_or_else(|| anyhow!("Failed to downcast JsonRpcError"))?; info!("Failed to send cancel transaction, assuming that is was already included by the other party...");
if json_rpc_err.code == TRANSACTION_ALREADY_IN_BLOCKCHAIN_ERROR_CODE { } else {
info!("Failed to send cancel transaction, assuming that is was already published by the other party..."); return Err(error);
}
} else { } else {
return Err(error); return Err(error);
} }

View file

@ -5,7 +5,7 @@ use crate::{
storage::Database, storage::Database,
SwapAmounts, TRANSACTION_ALREADY_IN_BLOCKCHAIN_ERROR_CODE, SwapAmounts, TRANSACTION_ALREADY_IN_BLOCKCHAIN_ERROR_CODE,
}; };
use anyhow::{anyhow, bail, Result}; use anyhow::{bail, Result};
use async_recursion::async_recursion; use async_recursion::async_recursion;
use bitcoin_harness::bitcoind_rpc::jsonrpc_client::JsonRpcError; use bitcoin_harness::bitcoind_rpc::jsonrpc_client::JsonRpcError;
use libp2p::{core::Multiaddr, PeerId}; use libp2p::{core::Multiaddr, PeerId};
@ -347,11 +347,12 @@ where
BobState::T1Expired(state4) => { BobState::T1Expired(state4) => {
let result = state4.submit_tx_cancel(bitcoin_wallet.as_ref()).await; let result = state4.submit_tx_cancel(bitcoin_wallet.as_ref()).await;
if let Err(error) = result { if let Err(error) = result {
let json_rpc_err = error if let Some(json_rpc_err) = error.downcast_ref::<JsonRpcError>() {
.downcast_ref::<JsonRpcError>() if json_rpc_err.code == TRANSACTION_ALREADY_IN_BLOCKCHAIN_ERROR_CODE {
.ok_or_else(|| anyhow!("Failed to downcast JsonRpcError"))?; info!("Failed to send cancel transaction, assuming that is was already included by the other party...");
if json_rpc_err.code == TRANSACTION_ALREADY_IN_BLOCKCHAIN_ERROR_CODE { } else {
info!("Failed to send cancel transaction, assuming that is was already included by the other party..."); return Err(error);
}
} else { } else {
return Err(error); return Err(error);
} }