fix: Allow the user to handle exceptions when pinging
@@ -516,7 +516,7 @@ public final class Client implements AutoCloseable {
*
* @param nodeAccountId Account ID of the node to ping
*/
- public Void ping(AccountId nodeAccountId) {
+ public Void ping(AccountId nodeAccountId) throws PrecheckStatusException, TimeoutException {
return ping(nodeAccountId, getRequestTimeout());
}
@@ -526,15 +526,11 @@ public final class Client implements AutoCloseable {
* @param nodeAccountId Account ID of the node to ping
* @param timeout The timeout after which the execution attempt will be cancelled.
*/
- public Void ping(AccountId nodeAccountId, Duration timeout) {
- try {
+ public Void ping(AccountId nodeAccountId, Duration timeout) throws PrecheckStatusException, TimeoutException {
new AccountBalanceQuery()
.setAccountId(nodeAccountId)
.setNodeAccountIds(Collections.singletonList(nodeAccountId))
.execute(this, timeout);
- } catch (Exception e) {
- logger.debug("pinging account {} failed with exception {}", nodeAccountId, e.getMessage());
- }
return null;
}
@@ -555,14 +551,19 @@ public final class Client implements AutoCloseable {
* @param timeout The timeout after which the execution attempt will be cancelled.
*/
public CompletableFuture<Void> pingAsync(AccountId nodeAccountId, Duration timeout) {
- return new AccountBalanceQuery()
+ var result = new CompletableFuture<Void>();
+ new AccountBalanceQuery()
.setAccountId(nodeAccountId)
.setNodeAccountIds(Collections.singletonList(nodeAccountId))
.executeAsync(this, timeout)
- .handle((balance, e) -> {
- // Do nothing
- return null;
+ .whenComplete((balance, error) -> {
+ if (error == null){
+ result.complete(null);
+ } else {
+ result.completeExceptionally(error);
+ }
});
+ return result;
}
/**
@@ -613,7 +614,7 @@ public final class Client implements AutoCloseable {
* Sends pings to all nodes in the client's network.
* Combines well with setMaxAttempts(1) to remove all dead nodes from the network.
*/
- public synchronized Void pingAll() {
+ public synchronized Void pingAll() throws PrecheckStatusException, TimeoutException {
return pingAll(getRequestTimeout());
}
@@ -623,7 +624,7 @@ public final class Client implements AutoCloseable {
*
* @param timeoutPerPing The timeout after which each execution attempt will be cancelled.
*/
- public synchronized Void pingAll(Duration timeoutPerPing) {
+ public synchronized Void pingAll(Duration timeoutPerPing) throws PrecheckStatusException, TimeoutException {
for (var nodeAccountId : network.getNetwork().values()) {
ping(nodeAccountId, timeoutPerPing);
}
