From d597ddab664bc287b86d56cad8ad67fd4e28d798 Mon Sep 17 00:00:00 2001 From: Jens Schulze Date: Wed, 4 Nov 2020 08:03:47 +0100 Subject: [PATCH] fix retry middleware to capture ApiHttpExceptions --- .../io/vrap/rmf/base/client/http/RetryMiddleware.java | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/rmf/rmf-java-base/src/main/java/io/vrap/rmf/base/client/http/RetryMiddleware.java b/rmf/rmf-java-base/src/main/java/io/vrap/rmf/base/client/http/RetryMiddleware.java index 04ac3bd94dd..f8eadf4ad19 100644 --- a/rmf/rmf-java-base/src/main/java/io/vrap/rmf/base/client/http/RetryMiddleware.java +++ b/rmf/rmf-java-base/src/main/java/io/vrap/rmf/base/client/http/RetryMiddleware.java @@ -1,7 +1,9 @@ package io.vrap.rmf.base.client.http; +import io.vrap.rmf.base.client.ApiHttpException; import io.vrap.rmf.base.client.ApiHttpRequest; import io.vrap.rmf.base.client.ApiHttpResponse; +import io.vrap.rmf.base.client.ClientFactory; import net.jodah.failsafe.Failsafe; import net.jodah.failsafe.FailsafeExecutor; import net.jodah.failsafe.RetryPolicy; @@ -18,7 +20,7 @@ import static java.util.Collections.singletonList; public class RetryMiddleware implements Middleware, AutoCloseable { - Logger logger = LoggerFactory.getLogger("commercetools"); + Logger logger = LoggerFactory.getLogger(ClientFactory.COMMERCETOOLS); private final ScheduledExecutorService executor = Executors.newScheduledThreadPool(2); private final FailsafeExecutor> failsafeExecutor; @@ -37,7 +39,12 @@ public RetryMiddleware(final int maxRetries, final long delay, final long maxDel public RetryMiddleware(final int maxRetries, final long delay, final long maxDelay, List statusCodes) { RetryPolicy> retryPolicy = new RetryPolicy>() - .handleResultIf(o -> statusCodes.contains(o.getStatusCode())) + .handleIf((response, throwable) -> { + if (throwable instanceof ApiHttpException) { + return statusCodes.contains(((ApiHttpException)throwable).getStatusCode()); + } + return statusCodes.contains(response.getStatusCode()); + }) .withBackoff(delay, maxDelay, ChronoUnit.MILLIS) .onRetry(event -> logger.info("Retry #" + event.getAttemptCount())) .withMaxRetries(maxRetries);