Gitlab Community Edition Instance

Commit 7b389ef5 authored by mhellka's avatar mhellka
Browse files

Unwrap CompletionException when translating between Promise and CompletableFuture

parent 3d3ae40c
......@@ -6,6 +6,7 @@ import java.util.Optional;
import java.util.concurrent.Callable;
import java.util.concurrent.CancellationException;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.CompletionException;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ExecutorService;
......@@ -148,7 +149,11 @@ public class Promise<T> {
*/
public static <V> Promise<V> wrap(CompletableFuture<V> future, ExecutorService pool) {
final Promise<V> p = new Promise<>(pool);
future.whenComplete(p::tryComplete);
future.whenComplete((result, error) -> {
while(error instanceof CompletionException && error.getCause() != null)
error = error.getCause();
p.tryComplete(result, error);
});
p.then(future::complete, future::completeExceptionally);
return p;
}
......@@ -160,8 +165,11 @@ public class Promise<T> {
*/
public CompletableFuture<T> toCompletableFuture() {
final CompletableFuture<T> cf = new CompletableFuture<>();
cf.whenComplete(this::tryComplete);
cf.whenComplete((result, error) -> {
while(error instanceof CompletionException && error.getCause() != null)
error = error.getCause();
tryComplete(result, error);
});
then(cf::complete, cf::completeExceptionally);
return cf;
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment