Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

map crt socket timeout exceptions to java connect timeout exceptions #5940

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
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 @@ -23,6 +23,7 @@
import static software.amazon.awssdk.utils.NumericUtils.saturatedCast;

import java.io.IOException;
import java.net.ConnectException;
import java.time.Duration;
import javax.net.ssl.SSLHandshakeException;
import software.amazon.awssdk.annotations.SdkInternalApi;
Expand All @@ -35,6 +36,7 @@
@SdkInternalApi
public final class CrtUtils {
public static final int CRT_TLS_NEGOTIATION_ERROR_CODE = 1029;
public static final int CRT_SOCKET_TIMEOUT = 1048;

private CrtUtils() {
}
Expand All @@ -54,9 +56,12 @@ public static Throwable wrapConnectionFailureException(Throwable throwable) {
Throwable toThrow = new IOException("An exception occurred when acquiring a connection", throwable);
if (throwable instanceof HttpException) {
HttpException httpException = (HttpException) throwable;
int httpErrorCode = httpException.getErrorCode();

if (httpException.getErrorCode() == CRT_TLS_NEGOTIATION_ERROR_CODE) {
if (httpErrorCode == CRT_TLS_NEGOTIATION_ERROR_CODE) {
toThrow = new SSLHandshakeException(httpException.getMessage());
} else if (httpErrorCode == CRT_SOCKET_TIMEOUT) {
toThrow = new ConnectException(httpException.getMessage());
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,13 @@
import static software.amazon.awssdk.http.crt.CrtHttpClientTestUtils.createRequest;

import java.io.IOException;
import java.net.ConnectException;
import java.net.URI;
import javax.net.ssl.SSLHandshakeException;
import java.util.concurrent.CompletableFuture;
import java.util.stream.Stream;
import java.util.AbstractMap.SimpleEntry;
import java.util.Map.Entry;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
Expand Down Expand Up @@ -59,6 +63,13 @@ public static Stream<Throwable> retryableExceptions() {
"connection closed"));
}

public static Stream<Entry<Integer, Class<? extends Throwable>>> mappedExceptions() {
return Stream.of(
new SimpleEntry<>(0x0405, SSLHandshakeException.class), // For AWS_IO_TLS_ERROR_NEGOTIATION_FAILURE (1029)
new SimpleEntry<>(0x0418, ConnectException.class) // For AWS_IO_SOCKET_TIMEOUT (1048)
);
}

@BeforeEach
public void setup() {
requestExecutor = new CrtRequestExecutor();
Expand Down Expand Up @@ -111,6 +122,23 @@ public void execute_AcquireConnectionFailure_shouldAlwaysWrapIOException() {
assertThatThrownBy(executeFuture::join).hasCauseInstanceOf(IOException.class).hasRootCause(exception);
}

@ParameterizedTest
@MethodSource("mappedExceptions")
public void execute_AcquireConnectionFailure_shouldAlwaysBeInstanceOfIOException(Entry<Integer, Class<? extends Throwable>> entry) {
int errorCode = entry.getKey();
Class<? extends Throwable> ioExceptionSubclass = entry.getValue();

CrtRequestContext context = crtRequestContext();
HttpException exception = new HttpException(errorCode);
CompletableFuture<HttpClientConnection> completableFuture = CompletableFutureUtils.failedFuture(exception);

Mockito.when(connectionManager.acquireConnection()).thenReturn(completableFuture);

CompletableFuture<SdkHttpFullResponse> executeFuture = requestExecutor.execute(context);
assertThatThrownBy(executeFuture::join).hasCauseInstanceOf(IOException.class).hasMessageContaining(exception.getMessage());
assertThatThrownBy(executeFuture::join).hasCauseInstanceOf(ioExceptionSubclass);
}

@Test
public void executeRequest_failedOfNonRetryableHttpException_shouldNotWrapIOException() {
HttpException exception = new HttpException(0x0801); // AWS_ERROR_HTTP_HEADER_NOT_FOUND
Expand Down