Consistently return non-zero exit codes for jarmode failures

Update jar mode launchers to catch all exceptions and return a non-zero
exit code. This refinement also allows us to consolidate the existing
error reporting logic to a central locations. Modes that wish to report
a simple error rather than a full stacktrace can throw the newly
introduced `JarModeErrorException`.

Fixes gh-43435
This commit is contained in:
Phillip Webb
2024-12-06 17:15:10 -08:00
parent 589697a011
commit f21402d4c3
18 changed files with 230 additions and 80 deletions

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2012-2023 the original author or authors.
* Copyright 2012-2024 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -33,6 +33,12 @@ class TestJarMode implements JarMode {
@Override
public void run(String mode, String[] args) {
System.out.println("running in " + mode + " jar mode " + Arrays.asList(args));
if (args.length > 0 && "error".equals(args[0])) {
throw new JarModeErrorException("error message");
}
if (args.length > 0 && "fail".equals(args[0])) {
throw new IllegalStateException("bad");
}
}
}

View File

@@ -63,6 +63,7 @@ class LauncherTests {
System.setProperty("jarmode", "test");
new TestLauncher().launch(new String[] { "boot" });
assertThat(out).contains("running in test jar mode [boot]");
assertThat(System.getProperty(JarModeRunner.SUPPRESSED_SYSTEM_EXIT_CODE)).isEqualTo("0");
}
@Test
@@ -70,6 +71,25 @@ class LauncherTests {
System.setProperty("jarmode", "idontexist");
new TestLauncher().launch(new String[] { "boot" });
assertThat(out).contains("Unsupported jarmode 'idontexist'");
assertThat(System.getProperty(JarModeRunner.SUPPRESSED_SYSTEM_EXIT_CODE)).isEqualTo("1");
}
@Test
void launchWhenJarModeRunFailsWithErrorExceptionPrintsSimpleMessage(CapturedOutput out) throws Exception {
System.setProperty("jarmode", "test");
new TestLauncher().launch(new String[] { "error" });
assertThat(out).contains("running in test jar mode [error]");
assertThat(out).contains("Error: error message");
assertThat(System.getProperty(JarModeRunner.SUPPRESSED_SYSTEM_EXIT_CODE)).isEqualTo("1");
}
@Test
void launchWhenJarModeRunFailsWithErrorExceptionPrintsStackTrace(CapturedOutput out) throws Exception {
System.setProperty("jarmode", "test");
new TestLauncher().launch(new String[] { "fail" });
assertThat(out).contains("running in test jar mode [fail]");
assertThat(out).contains("java.lang.IllegalStateException: bad");
assertThat(System.getProperty(JarModeRunner.SUPPRESSED_SYSTEM_EXIT_CODE)).isEqualTo("1");
}
}