Skip to content

Commit

Permalink
handle potentially unfulfilled CompletableFuture
Browse files Browse the repository at this point in the history
  • Loading branch information
reugn committed Jul 21, 2024
1 parent 541cd74 commit e8a0599
Showing 1 changed file with 10 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,11 @@ public CompletableFuture<String> getValueFuture() {

@Override
public void onSuccess(Map<String, String> map) {
stringValueFuture.complete(map.get(command));
try {
stringValueFuture.complete(map.get(command));
} catch (Exception e) {
stringValueFuture.completeExceptionally(commandFailed(command, e));
}
}

@Override
Expand All @@ -44,20 +48,20 @@ public void onFailure(AerospikeException ae) {
try {
client.info(client.getCluster().eventLoops.next(), listener, infoPolicy, node, command);
} catch (AerospikeException ae) {
fail(command, ae);
throw commandFailed(command, ae);
}

String value = null;
String value;
try {
value = listener.getValueFuture().orTimeout(infoPolicy.timeout, TimeUnit.MILLISECONDS).join();
} catch (CompletionException ce) {
fail(command, ce.getCause());
throw commandFailed(command, ce.getCause());
}
return value == null ? "" : value;
}

private static void fail(String command, Throwable t) {
throw new AerospikeException(String.format("Info command %s failed", command), t);
private static AerospikeException commandFailed(String command, Throwable t) {
return new AerospikeException(String.format("Info command %s failed", command), t);
}

interface InfoListenerWithStringValue extends InfoListener {
Expand Down

0 comments on commit e8a0599

Please sign in to comment.