From 25d4b8b8a01bc746f591ee5b3681c681e8cd7894 Mon Sep 17 00:00:00 2001 From: Sean Gilligan Date: Fri, 25 Mar 2022 19:03:22 +0100 Subject: [PATCH] WalletTool: improve sendPayment() exception handling * Split the long `try` block into multiple `try/catch` blocks * Consistently use System.err.println and System.exit on errors * System.out.println an information message before falling back to `broadcastPayment` --- .../org/bitcoinj/wallettool/WalletTool.java | 33 +++++++++++++------ 1 file changed, 23 insertions(+), 10 deletions(-) diff --git a/wallettool/src/main/java/org/bitcoinj/wallettool/WalletTool.java b/wallettool/src/main/java/org/bitcoinj/wallettool/WalletTool.java index 6e9e6b540..1cfbe5723 100644 --- a/wallettool/src/main/java/org/bitcoinj/wallettool/WalletTool.java +++ b/wallettool/src/main/java/org/bitcoinj/wallettool/WalletTool.java @@ -885,13 +885,28 @@ public class WalletTool implements Callable { return; // Error message already printed. } + // Complete the transaction try { wallet.completeTx(req); // may throw InsufficientMoneyException. - if (offline) { - wallet.commitTx(req.tx); - return; - } + } catch (InsufficientMoneyException e) { + System.err.println("Insufficient funds: have " + wallet.getBalance().toFriendlyString()); + System.exit(1); + } + if (offline) { + wallet.commitTx(req.tx); + return; + } + + // Setup network communication (but not PeerGroup) + try { setup(); + } catch (BlockStoreException e) { + System.err.println("BlockStoreException: " + e.getMessage()); + System.exit(1); + } + + // Send the payment + try { // No refund address specified, no user-specified memo field. PaymentProtocol.Ack ack = session.sendPayment(ImmutableList.of(req.tx), null, null).get(); wallet.commitTx(req.tx); @@ -900,6 +915,7 @@ public class WalletTool implements Callable { try { throw e.getCause(); } catch (PaymentProtocolException.InvalidPaymentRequestURL e1) { + System.out.println("Missing/Invalid Payment Request URL, broadcasting transaction with PeerGroup"); broadcastPayment(req); } catch (PaymentProtocolException e1) { System.err.println("Failed to send payment " + e.getMessage()); @@ -914,12 +930,9 @@ public class WalletTool implements Callable { } catch (VerificationException e) { System.err.println("Failed to send payment " + e.getMessage()); System.exit(1); - } catch (InterruptedException e1) { - // Ignore. - } catch (InsufficientMoneyException e) { - System.err.println("Insufficient funds: have " + wallet.getBalance().toFriendlyString()); - } catch (BlockStoreException e) { - throw new RuntimeException(e); + } catch (InterruptedException e) { + System.err.println("Interrupted: " + e.getMessage()); + System.exit(1); } }