Skip to content

Commit

Permalink
wrap exceptions thrown by async transport implementations in IOException
Browse files Browse the repository at this point in the history
Signed-off-by: Andrew Parmet <[email protected]>
  • Loading branch information
andrewparmet committed Oct 6, 2023
1 parent 57dc5cf commit a29a5d1
Show file tree
Hide file tree
Showing 6 changed files with 83 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -194,17 +194,11 @@ public <RequestT, ResponseT, ErrorT> ResponseT performRequest(
try {
return executeAsync((SdkAsyncHttpClient) httpClient, clientReq, requestBody, endpoint, options).get();
} catch (ExecutionException e) {
Throwable cause = e.getCause();
if (cause != null) {
if (cause instanceof IOException) {
throw (IOException) cause;
}
if (cause instanceof RuntimeException) {
throw (RuntimeException) cause;
}
throw new RuntimeException(cause);
if (e.getCause() instanceof IOException) {
throw (IOException) e.getCause();
} else {
throw new IOException(e.getCause());
}
throw new RuntimeException(e);
} catch (InterruptedException e) {
throw new IOException("HttpRequest was interrupted", e);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -143,9 +143,7 @@ public <RequestT, ResponseT, ErrorT> ResponseT performRequest(
try {
return performRequestAsync(request, endpoint, options).join();
} catch (final CompletionException ex) {
if (ex.getCause() instanceof RuntimeException) {
throw (RuntimeException) ex.getCause();
} else if (ex.getCause() instanceof IOException) {
if (ex.getCause() instanceof IOException) {
throw (IOException) ex.getCause();
} else {
throw new IOException(ex.getCause());
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package org.opensearch.client.opensearch.integTest;

import org.apache.logging.log4j.core.util.Throwables;
import org.junit.Test;

public abstract class AbstractAsyncStracktraceIT extends OpenSearchJavaClientTestCase {
@Test
public void testFailureFromClientPreservesStacktraceOfCaller() throws Exception {
var thrown = assertThrows(Exception.class, () -> javaClient().indices().get(g -> g.index("nonexisting-index")));

var stacktraceElements = Throwables.toStringList(thrown);

stacktraceElements.forEach(System.out::println);

var someElementContainsCallerMethodName =
stacktraceElements.stream().anyMatch(it -> it.contains("testFailureFromClientPreservesStacktraceOfCaller"));

assertTrue(someElementContainsCallerMethodName);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package org.opensearch.client.opensearch.integTest.aws;

import org.opensearch.client.opensearch.integTest.AbstractAsyncStracktraceIT;

public class AwsSdk2AsyncStacktraceIT extends AbstractAsyncStracktraceIT implements AwsSdk2AsyncTransportSupport {
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
package org.opensearch.client.opensearch.integTest.aws;

import org.apache.hc.core5.http.HttpHost;
import org.opensearch.client.opensearch.integTest.OpenSearchTransportSupport;
import org.opensearch.client.transport.OpenSearchTransport;
import org.opensearch.client.transport.aws.AwsSdk2Transport;
import org.opensearch.client.transport.aws.AwsSdk2TransportOptions;
import org.opensearch.common.settings.Settings;
import software.amazon.awssdk.http.async.SdkAsyncHttpClient;
import software.amazon.awssdk.http.crt.AwsCrtAsyncHttpClient;
import software.amazon.awssdk.regions.Region;

import java.io.IOException;

interface AwsSdk2AsyncTransportSupport extends OpenSearchTransportSupport {
@Override
default OpenSearchTransport buildTransport(Settings settings, HttpHost[] hosts) throws IOException {
return new AwsSdk2Transport(
getAsyncHttpClient(),
getTestClusterHost(),
getTestClusterServiceName(),
getTestClusterRegion(),
getTransportOptions().build());
}

private String getTestClusterHost() {
return System.getProperty("tests.awsSdk2support.domainHost");
}

private String getTestClusterServiceName() {
return System.getProperty("tests.awsSdk2support.serviceName");
}

private Region getTestClusterRegion() {
String region = System.getProperty("tests.awsSdk2support.domainRegion");
return region != null ? Region.of(region) : Region.US_EAST_1;
}

private AwsSdk2TransportOptions.Builder getTransportOptions() {
return AwsSdk2TransportOptions.builder();
}

private SdkAsyncHttpClient getAsyncHttpClient() {
return AwsCrtAsyncHttpClient.create();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package org.opensearch.client.opensearch.integTest.httpclient5;

import org.opensearch.client.opensearch.integTest.AbstractAsyncStracktraceIT;

public class AsyncStacktraceIT extends AbstractAsyncStracktraceIT implements HttpClient5TransportSupport {
}

0 comments on commit a29a5d1

Please sign in to comment.