Skip to content

Commit

Permalink
JAVA-3033 - Consider handling IOException in a manner consistent with…
Browse files Browse the repository at this point in the history
… other exceptions in CloudConfigFactory.fetchProxyMetadata()
  • Loading branch information
Mike Zhang authored and hhughes committed Jul 27, 2023
1 parent ec93ef9 commit 680c7db
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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",
Expand All @@ -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);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 680c7db

Please sign in to comment.