Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
import java.util.OptionalLong;
import java.util.Set;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.CompletionException;
import java.util.concurrent.ConcurrentLinkedQueue;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.RejectedExecutionException;
Expand Down Expand Up @@ -185,7 +186,7 @@ public void send(
.whenComplete(
(httpResponse, throwable) -> {
if (throwable != null) {
onError.accept(throwable);
onError.accept(unwrap(throwable));
return;
}
onResponse.accept(httpResponse);
Expand All @@ -196,6 +197,19 @@ public void send(
}
}

// supplyAsync wraps failures in CompletionException, and IOExceptions are additionally wrapped in
// UncheckedIOException above. Pass the original exception to onError, as OkHttpHttpSender does.
private static Throwable unwrap(Throwable throwable) {
Throwable cause = throwable;
if (cause instanceof CompletionException && cause.getCause() != null) {
cause = cause.getCause();
}
if (cause instanceof UncheckedIOException && cause.getCause() != null) {
cause = cause.getCause();
}
return cause;
}

// Visible for testing
HttpResponse sendInternal(MessageWriter requestBodyWriter) throws IOException {
long startTimeNanos = System.nanoTime();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -327,7 +327,8 @@ void send_successfulResponse_callsOnResponse() throws Exception {

@Test
void send_ioException_callsOnError() throws Exception {
doThrow(new IOException("send failed")).when(mockHttpClient).send(any(), any());
IOException exception = new IOException("send failed");
doThrow(exception).when(mockHttpClient).send(any(), any());

JdkHttpSender testSender =
new JdkHttpSender(
Expand Down Expand Up @@ -358,9 +359,7 @@ void send_ioException_callsOnError() throws Exception {
});

assertThat(latch.await(5, TimeUnit.SECONDS)).isTrue();
assertThat(errorRef.get()).isNotNull();
assertThat(errorRef.get()).hasRootCauseInstanceOf(IOException.class);
assertThat(errorRef.get()).hasRootCauseMessage("send failed");
assertThat(errorRef.get()).isSameAs(exception);
assertThat(responseRef.get()).isNull();
} finally {
testSender.shutdown();
Expand Down
Loading