diff --git a/java-client/src/main/java/org/opensearch/client/transport/aws/AwsSdk2Transport.java b/java-client/src/main/java/org/opensearch/client/transport/aws/AwsSdk2Transport.java index eb3274a368..737de115ff 100644 --- a/java-client/src/main/java/org/opensearch/client/transport/aws/AwsSdk2Transport.java +++ b/java-client/src/main/java/org/opensearch/client/transport/aws/AwsSdk2Transport.java @@ -194,11 +194,15 @@ public ResponseT performRequest( try { return executeAsync((SdkAsyncHttpClient) httpClient, clientReq, requestBody, endpoint, options).get(); } catch (ExecutionException e) { - if (e.getCause() instanceof IOException) { - throw (IOException) e.getCause(); - } else { - throw new IOException(e.getCause()); + Throwable cause = e.getCause(); + if (cause != null) { + if (cause instanceof IOException) { + throw (IOException) cause; + } else { + throw new IOException(cause); + } } + throw new IOException(e); } catch (InterruptedException e) { throw new IOException("HttpRequest was interrupted", e); } diff --git a/java-client/src/main/java/org/opensearch/client/transport/httpclient5/ApacheHttpClient5Transport.java b/java-client/src/main/java/org/opensearch/client/transport/httpclient5/ApacheHttpClient5Transport.java index 8d3b14c9dd..032e0fd204 100644 --- a/java-client/src/main/java/org/opensearch/client/transport/httpclient5/ApacheHttpClient5Transport.java +++ b/java-client/src/main/java/org/opensearch/client/transport/httpclient5/ApacheHttpClient5Transport.java @@ -143,11 +143,15 @@ public ResponseT performRequest( try { return performRequestAsync(request, endpoint, options).join(); } catch (final CompletionException ex) { - if (ex.getCause() instanceof IOException) { - throw (IOException) ex.getCause(); - } else { - throw new IOException(ex.getCause()); + Throwable cause = ex.getCause(); + if (cause != null) { + if (cause instanceof IOException) { + throw (IOException) cause; + } else { + throw new IOException(cause); + } } + throw new IOException(ex); } }