diff --git a/resilience4j-timelimiter/src/main/java/io/github/resilience4j/timelimiter/TimeLimiter.java b/resilience4j-timelimiter/src/main/java/io/github/resilience4j/timelimiter/TimeLimiter.java index 0dde23c67c..87b02ed8d5 100644 --- a/resilience4j-timelimiter/src/main/java/io/github/resilience4j/timelimiter/TimeLimiter.java +++ b/resilience4j-timelimiter/src/main/java/io/github/resilience4j/timelimiter/TimeLimiter.java @@ -3,10 +3,7 @@ import io.github.resilience4j.timelimiter.internal.TimeLimiterImpl; import java.time.Duration; -import java.util.concurrent.Callable; -import java.util.concurrent.Future; -import java.util.concurrent.TimeUnit; -import java.util.concurrent.TimeoutException; +import java.util.concurrent.*; import java.util.function.Supplier; /** @@ -66,6 +63,15 @@ static > Callable decorateFutureSupplier(TimeLimiter t future.cancel(true); } throw e; + } catch (ExecutionException e){ + Throwable t = e.getCause(); + if (t == null){ + throw e; + } + if (t instanceof Error){ + throw (Error) t; + } + throw (Exception) t; } }; } diff --git a/resilience4j-timelimiter/src/test/java/io/github/resilience4j/timelimiter/TimeLimiterTest.java b/resilience4j-timelimiter/src/test/java/io/github/resilience4j/timelimiter/TimeLimiterTest.java index 917e971699..0c5c6ab1cc 100644 --- a/resilience4j-timelimiter/src/test/java/io/github/resilience4j/timelimiter/TimeLimiterTest.java +++ b/resilience4j-timelimiter/src/test/java/io/github/resilience4j/timelimiter/TimeLimiterTest.java @@ -79,4 +79,17 @@ public void shouldReturnResult() throws Exception { result = timeLimiter.decorateFutureSupplier(supplier).call(); Assertions.assertThat(result).isEqualTo(42); } + + @Test + public void unwrapExecutionException() { + TimeLimiter timeLimiter = TimeLimiter.ofDefaults(); + ExecutorService executorService = Executors.newSingleThreadExecutor(); + + Supplier> supplier = () -> executorService.submit(() -> {throw new RuntimeException();}); + Callable decorated = TimeLimiter.decorateFutureSupplier(timeLimiter, supplier); + + Try decoratedResult = Try.ofCallable(decorated); + + Assertions.assertThat(decoratedResult.getCause() instanceof RuntimeException).isTrue(); + } }