Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 5 additions & 5 deletions src/main/java/co/elastic/support/rest/RestClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -86,10 +86,10 @@ private HttpResponse execRequest(HttpRequestBase httpRequest) {
return client.execute(httpHost, httpRequest, httpContext);
} catch (HttpHostConnectException e) {
logger.error("Host connection error.", e);
throw new RuntimeException("Host connection");
throw new RuntimeException("Host connection failed", e);
} catch (Exception e) {
logger.error("Unexpected Execution Error", e);
throw new RuntimeException(e.getMessage());
throw new RuntimeException("Unexpected error during HTTP execution", e);
}
}

Expand All @@ -104,7 +104,7 @@ public HttpResponse execPost(String uri, String payload) {
return execRequest(httpPost);
} catch (UnsupportedEncodingException e) {
logger.error(Constants.CONSOLE, "Error with json body.", e);
throw new RuntimeException("Could not complete post request.");
throw new RuntimeException("Could not complete post request.", e);
}
}

Expand All @@ -121,7 +121,7 @@ public void close() {
client.close();
}
} catch (Exception e) {
logger.error("Error occurred closing client connection.");
logger.error("Error occurred closing client connection.", e);
}
}

Expand Down Expand Up @@ -229,4 +229,4 @@ public static RestClient getClient(
throw new RuntimeException("Error establishing http connection for: " + host, e);
}
}
}
}
27 changes: 27 additions & 0 deletions src/test/java/co/elastic/support/rest/TestRestExecCalls.java
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@
import static com.github.tomakehurst.wiremock.client.WireMock.urlEqualTo;
import static com.github.tomakehurst.wiremock.core.WireMockConfiguration.wireMockConfig;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.assertTrue;

@TestInstance(TestInstance.Lifecycle.PER_CLASS)
Expand Down Expand Up @@ -189,6 +191,31 @@ public void testAuthFailure() {
assertTrue(fileExistsWithText(targetFilename, "autherror_response_body"));
}

@Test
public void testConnectionRefusedPreservesCause() {
RestClient unreachableClient = RestClient.getClient(
"localhost",
19999,
"http",
"elastic",
"elastic",
"",
0,
"",
"",
"",
"",
true,
Collections.emptyMap(),
1000,
1000,
1000);

RuntimeException ex = assertThrows(RuntimeException.class,
() -> unreachableClient.execGet("/"));
assertNotNull(ex.getCause(), "RuntimeException must preserve original cause");
}

private boolean fileExistsWithText(String filename, String compare) {
try {
String fileContents = FileUtils.readFileToString(new File(filename), "UTF8");
Expand Down