Skip to content
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,7 @@ public static void main(final String[] args) throws Exception {
LogConfigurator.registerErrorListener();
final OpenSearch opensearch = new OpenSearch();
int status = main(args, opensearch, Terminal.DEFAULT);

if (status != ExitCodes.OK) {
final String basePath = System.getProperty("opensearch.logs.base_path");
// It's possible to fail before logging has been configured, in which case there's no point
Expand Down Expand Up @@ -122,7 +123,14 @@ private static void overrideDnsCachePolicyProperties() {
}

static int main(final String[] args, final OpenSearch opensearch, final Terminal terminal) throws Exception {
return opensearch.main(args, terminal);
try {
return opensearch.main(args, terminal);
} catch (StartupException e) {
// StartupException has custom printStackTrace formatting (truncates guice frames, etc.).
// Catch it here so the process exits rather than hanging, while preserving that output.
e.printStackTrace(terminal.getErrorWriter());
return ExitCodes.CODE_ERROR;
}
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -226,4 +226,18 @@ public void testUnknownOption() throws Exception {
);
}

public void testStartupExceptionExitsWithCodeError() throws Exception {
// StartupException is caught in the 3-arg main(), which prints it via its custom formatter
// and returns CODE_ERROR so that the 1-arg main() calls exit(CODE_ERROR).
final RuntimeException cause = new RuntimeException("dummy startup failure");
runTest(
ExitCodes.CODE_ERROR,
true,
(output, error) -> assertThat(error, containsString(cause.getMessage())),
(foreground, pidFile, quiet, env) -> {
throw new StartupException(cause);
}
);
}

}
Loading