mirror of
https://github.com/bitcoinj/bitcoinj.git
synced 2025-02-23 06:35:17 +01:00
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`
This commit is contained in:
parent
091fdd9791
commit
25d4b8b8a0
1 changed files with 23 additions and 10 deletions
|
@ -885,13 +885,28 @@ public class WalletTool implements Callable<Integer> {
|
||||||
return; // Error message already printed.
|
return; // Error message already printed.
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Complete the transaction
|
||||||
try {
|
try {
|
||||||
wallet.completeTx(req); // may throw InsufficientMoneyException.
|
wallet.completeTx(req); // may throw InsufficientMoneyException.
|
||||||
|
} catch (InsufficientMoneyException e) {
|
||||||
|
System.err.println("Insufficient funds: have " + wallet.getBalance().toFriendlyString());
|
||||||
|
System.exit(1);
|
||||||
|
}
|
||||||
if (offline) {
|
if (offline) {
|
||||||
wallet.commitTx(req.tx);
|
wallet.commitTx(req.tx);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Setup network communication (but not PeerGroup)
|
||||||
|
try {
|
||||||
setup();
|
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.
|
// No refund address specified, no user-specified memo field.
|
||||||
PaymentProtocol.Ack ack = session.sendPayment(ImmutableList.of(req.tx), null, null).get();
|
PaymentProtocol.Ack ack = session.sendPayment(ImmutableList.of(req.tx), null, null).get();
|
||||||
wallet.commitTx(req.tx);
|
wallet.commitTx(req.tx);
|
||||||
|
@ -900,6 +915,7 @@ public class WalletTool implements Callable<Integer> {
|
||||||
try {
|
try {
|
||||||
throw e.getCause();
|
throw e.getCause();
|
||||||
} catch (PaymentProtocolException.InvalidPaymentRequestURL e1) {
|
} catch (PaymentProtocolException.InvalidPaymentRequestURL e1) {
|
||||||
|
System.out.println("Missing/Invalid Payment Request URL, broadcasting transaction with PeerGroup");
|
||||||
broadcastPayment(req);
|
broadcastPayment(req);
|
||||||
} catch (PaymentProtocolException e1) {
|
} catch (PaymentProtocolException e1) {
|
||||||
System.err.println("Failed to send payment " + e.getMessage());
|
System.err.println("Failed to send payment " + e.getMessage());
|
||||||
|
@ -914,12 +930,9 @@ public class WalletTool implements Callable<Integer> {
|
||||||
} catch (VerificationException e) {
|
} catch (VerificationException e) {
|
||||||
System.err.println("Failed to send payment " + e.getMessage());
|
System.err.println("Failed to send payment " + e.getMessage());
|
||||||
System.exit(1);
|
System.exit(1);
|
||||||
} catch (InterruptedException e1) {
|
} catch (InterruptedException e) {
|
||||||
// Ignore.
|
System.err.println("Interrupted: " + e.getMessage());
|
||||||
} catch (InsufficientMoneyException e) {
|
System.exit(1);
|
||||||
System.err.println("Insufficient funds: have " + wallet.getBalance().toFriendlyString());
|
|
||||||
} catch (BlockStoreException e) {
|
|
||||||
throw new RuntimeException(e);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Add table
Reference in a new issue