Skip to content

Commit 8a2baf5

Browse files
authored
Throw Auth0Exception when failure occurs during retries (#495)
1 parent 1639102 commit 8a2baf5

2 files changed

Lines changed: 30 additions & 1 deletion

File tree

src/main/java/com/auth0/net/RateLimitInterceptor.java

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
package com.auth0.net;
22

3+
import com.auth0.exception.Auth0Exception;
34
import com.auth0.net.client.DefaultHttpClient;
45
import net.jodah.failsafe.Failsafe;
6+
import net.jodah.failsafe.FailsafeException;
57
import net.jodah.failsafe.RetryPolicy;
68
import net.jodah.failsafe.event.ExecutionAttemptedEvent;
79
import net.jodah.failsafe.function.CheckedConsumer;
@@ -78,6 +80,12 @@ public Response intercept(@NotNull Chain chain) throws IOException {
7880
retryPolicy.onRetry(retryListener);
7981
}
8082

81-
return Failsafe.with(retryPolicy).get(() -> chain.proceed(chain.request()));
83+
try {
84+
// throw Auth0Exception instead of FailSafe exception on error
85+
// see https://github.com/auth0/auth0-java/issues/483
86+
return Failsafe.with(retryPolicy).get(() -> chain.proceed(chain.request()));
87+
} catch (FailsafeException fe) {
88+
throw new Auth0Exception("Failed to execute request", fe.getCause());
89+
}
8290
}
8391
}

src/test/java/com/auth0/net/RateLimitInterceptorTest.java

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,26 @@
11
package com.auth0.net;
22

3+
import com.auth0.exception.Auth0Exception;
34
import okhttp3.OkHttpClient;
45
import okhttp3.Request;
56
import okhttp3.Response;
67
import okhttp3.mockwebserver.MockResponse;
78
import okhttp3.mockwebserver.MockWebServer;
89
import okhttp3.mockwebserver.RecordedRequest;
10+
import okhttp3.mockwebserver.SocketPolicy;
911
import org.junit.After;
1012
import org.junit.Before;
1113
import org.junit.Test;
1214

15+
import java.time.Duration;
1316
import java.util.ArrayList;
1417
import java.util.List;
1518

1619
import static org.hamcrest.CoreMatchers.is;
1720
import static org.hamcrest.MatcherAssert.assertThat;
1821
import static org.hamcrest.Matchers.closeTo;
1922
import static org.hamcrest.Matchers.greaterThan;
23+
import static org.junit.Assert.assertThrows;
2024

2125
public class RateLimitInterceptorTest {
2226

@@ -175,4 +179,21 @@ public void shouldBackOffOnRetries() throws Exception {
175179
assertThat(retryTimings.get(5), greaterThan(retryTimings.get(2)));
176180
}
177181

182+
@Test
183+
public void shouldThrowAuth0Exception() {
184+
OkHttpClient client = new OkHttpClient.Builder()
185+
.addInterceptor(new RateLimitInterceptor(3))
186+
.readTimeout(Duration.ofSeconds(1))
187+
.build();
188+
189+
server.enqueue(new MockResponse().setSocketPolicy(SocketPolicy.NO_RESPONSE));
190+
191+
okhttp3.Request request = new Request.Builder()
192+
.get()
193+
.url(server.url("/"))
194+
.build();
195+
196+
Auth0Exception e = assertThrows(Auth0Exception.class, () -> client.newCall(request).execute());
197+
assertThat(e.getMessage(), is("Failed to execute request"));
198+
}
178199
}

0 commit comments

Comments
 (0)