diff --git a/core/src/main/java/com/datastax/oss/driver/internal/core/config/cloud/CloudConfigFactory.java b/core/src/main/java/com/datastax/oss/driver/internal/core/config/cloud/CloudConfigFactory.java index f7386dcc390..5aea1392d35 100644 --- a/core/src/main/java/com/datastax/oss/driver/internal/core/config/cloud/CloudConfigFactory.java +++ b/core/src/main/java/com/datastax/oss/driver/internal/core/config/cloud/CloudConfigFactory.java @@ -24,12 +24,7 @@ import com.fasterxml.jackson.databind.JsonNode; import com.fasterxml.jackson.databind.ObjectMapper; import edu.umd.cs.findbugs.annotations.NonNull; -import java.io.BufferedReader; -import java.io.ByteArrayInputStream; -import java.io.ByteArrayOutputStream; -import java.io.IOException; -import java.io.InputStream; -import java.io.InputStreamReader; +import java.io.*; import java.net.ConnectException; import java.net.InetSocketAddress; import java.net.MalformedURLException; @@ -228,8 +223,14 @@ protected BufferedReader fetchProxyMetadata( connection.setSSLSocketFactory(sslContext.getSocketFactory()); connection.setRequestMethod("GET"); connection.setRequestProperty("host", "localhost"); - return new BufferedReader( - new InputStreamReader(connection.getInputStream(), StandardCharsets.UTF_8)); + try { + return new BufferedReader( + new InputStreamReader(connection.getInputStream(), StandardCharsets.UTF_8)); + } catch (IOException ioe) { + throw new IllegalStateException( + "Unable to read data from cloud metadata service. Please make sure your cluster is not parked or terminated", + ioe); + } } catch (ConnectException e) { throw new IllegalStateException( "Unable to connect to cloud metadata service. Please make sure your cluster is not parked or terminated", @@ -238,6 +239,10 @@ protected BufferedReader fetchProxyMetadata( throw new IllegalStateException( "Unable to resolve host for cloud metadata service. Please make sure your cluster is not terminated", e); + } catch (FileNotFoundException e) { + throw new IllegalStateException( + "Metadata service request path not found. Please make sure the metadata service url is correct", + e); } } diff --git a/core/src/test/java/com/datastax/oss/driver/internal/core/config/cloud/CloudConfigFactoryTest.java b/core/src/test/java/com/datastax/oss/driver/internal/core/config/cloud/CloudConfigFactoryTest.java index 6f54413b601..7f1da159941 100644 --- a/core/src/test/java/com/datastax/oss/driver/internal/core/config/cloud/CloudConfigFactoryTest.java +++ b/core/src/test/java/com/datastax/oss/driver/internal/core/config/cloud/CloudConfigFactoryTest.java @@ -35,8 +35,7 @@ import com.github.tomakehurst.wiremock.jetty9.JettyHttpServer; import com.github.tomakehurst.wiremock.junit.WireMockRule; import com.google.common.base.Joiner; -import java.io.FileNotFoundException; -import java.io.IOException; +import java.io.*; import java.net.InetSocketAddress; import java.net.URISyntaxException; import java.net.URL; @@ -134,7 +133,19 @@ public void should_throw_when_metadata_not_found() throws Exception { URL configFile = new URL("http", "localhost", wireMockRule.port(), BUNDLE_PATH); CloudConfigFactory cloudConfigFactory = new CloudConfigFactory(); Throwable t = catchThrowable(() -> cloudConfigFactory.createCloudConfig(configFile)); - assertThat(t).isInstanceOf(FileNotFoundException.class).hasMessageContaining("metadata"); + assertThat(t).isInstanceOf(IllegalStateException.class).hasMessageContaining("metadata"); + } + + @Test + public void should_throw_when_stream_io_error_fetch_metadata() throws Exception { + // given + mockHttpSecureBundle(secureBundle()); + stubFor(any(urlPathEqualTo("/metadata")).willReturn(aResponse().withStatus(401))); + // when + URL configFile = new URL("http", "localhost", wireMockRule.port(), BUNDLE_PATH); + CloudConfigFactory cloudConfigFactory = new CloudConfigFactory(); + Throwable t = catchThrowable(() -> cloudConfigFactory.createCloudConfig(configFile)); + assertThat(t).isInstanceOf(IllegalStateException.class).hasMessageContaining("metadata"); } @Test