Skip to content

Commit 1639b81

Browse files
author
Mike Zhang
committed
JAVA-3033 - Consider handling IOException in a manner consistent with other exceptions in CloudConfigFactory.fetchProxyMetadata()
1 parent f6cc4d7 commit 1639b81

2 files changed

Lines changed: 27 additions & 11 deletions

File tree

core/src/main/java/com/datastax/oss/driver/internal/core/config/cloud/CloudConfigFactory.java

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -24,12 +24,7 @@
2424
import com.fasterxml.jackson.databind.JsonNode;
2525
import com.fasterxml.jackson.databind.ObjectMapper;
2626
import edu.umd.cs.findbugs.annotations.NonNull;
27-
import java.io.BufferedReader;
28-
import java.io.ByteArrayInputStream;
29-
import java.io.ByteArrayOutputStream;
30-
import java.io.IOException;
31-
import java.io.InputStream;
32-
import java.io.InputStreamReader;
27+
import java.io.*;
3328
import java.net.ConnectException;
3429
import java.net.InetSocketAddress;
3530
import java.net.MalformedURLException;
@@ -228,8 +223,14 @@ protected BufferedReader fetchProxyMetadata(
228223
connection.setSSLSocketFactory(sslContext.getSocketFactory());
229224
connection.setRequestMethod("GET");
230225
connection.setRequestProperty("host", "localhost");
231-
return new BufferedReader(
232-
new InputStreamReader(connection.getInputStream(), StandardCharsets.UTF_8));
226+
try {
227+
return new BufferedReader(
228+
new InputStreamReader(connection.getInputStream(), StandardCharsets.UTF_8));
229+
} catch (IOException ioe) {
230+
throw new IllegalStateException(
231+
"Unable to read data from cloud metadata service. Please make sure your cluster is not parked or terminated",
232+
ioe);
233+
}
233234
} catch (ConnectException e) {
234235
throw new IllegalStateException(
235236
"Unable to connect to cloud metadata service. Please make sure your cluster is not parked or terminated",
@@ -238,6 +239,10 @@ protected BufferedReader fetchProxyMetadata(
238239
throw new IllegalStateException(
239240
"Unable to resolve host for cloud metadata service. Please make sure your cluster is not terminated",
240241
e);
242+
} catch (FileNotFoundException e) {
243+
throw new IllegalStateException(
244+
"Metadata service request path not found. Please make sure the metadata service url is correct",
245+
e);
241246
}
242247
}
243248

core/src/test/java/com/datastax/oss/driver/internal/core/config/cloud/CloudConfigFactoryTest.java

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,7 @@
3535
import com.github.tomakehurst.wiremock.jetty9.JettyHttpServer;
3636
import com.github.tomakehurst.wiremock.junit.WireMockRule;
3737
import com.google.common.base.Joiner;
38-
import java.io.FileNotFoundException;
39-
import java.io.IOException;
38+
import java.io.*;
4039
import java.net.InetSocketAddress;
4140
import java.net.URISyntaxException;
4241
import java.net.URL;
@@ -134,7 +133,19 @@ public void should_throw_when_metadata_not_found() throws Exception {
134133
URL configFile = new URL("http", "localhost", wireMockRule.port(), BUNDLE_PATH);
135134
CloudConfigFactory cloudConfigFactory = new CloudConfigFactory();
136135
Throwable t = catchThrowable(() -> cloudConfigFactory.createCloudConfig(configFile));
137-
assertThat(t).isInstanceOf(FileNotFoundException.class).hasMessageContaining("metadata");
136+
assertThat(t).isInstanceOf(IllegalStateException.class).hasMessageContaining("metadata");
137+
}
138+
139+
@Test
140+
public void should_throw_when_stream_io_error_fetch_metadata() throws Exception {
141+
// given
142+
mockHttpSecureBundle(secureBundle());
143+
stubFor(any(urlPathEqualTo("/metadata")).willReturn(aResponse().withStatus(401)));
144+
// when
145+
URL configFile = new URL("http", "localhost", wireMockRule.port(), BUNDLE_PATH);
146+
CloudConfigFactory cloudConfigFactory = new CloudConfigFactory();
147+
Throwable t = catchThrowable(() -> cloudConfigFactory.createCloudConfig(configFile));
148+
assertThat(t).isInstanceOf(IllegalStateException.class).hasMessageContaining("metadata");
138149
}
139150

140151
@Test

0 commit comments

Comments
 (0)