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:
@@ -55,6 +55,7 @@ class LauncherJarModeTests {
|
||||
System.setProperty("jarmode", "test");
|
||||
new TestLauncher().launch(new String[] { "boot" });
|
||||
assertThat(out).contains("running in test jar mode [boot]");
|
||||
assertThat(System.getProperty(JarModeLauncher.SUPPRESSED_SYSTEM_EXIT_CODE)).isEqualTo("0");
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -62,6 +63,25 @@ class LauncherJarModeTests {
|
||||
System.setProperty("jarmode", "idontexist");
|
||||
new TestLauncher().launch(new String[] { "boot" });
|
||||
assertThat(out).contains("Unsupported jarmode 'idontexist'");
|
||||
assertThat(System.getProperty(JarModeLauncher.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(JarModeLauncher.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(JarModeLauncher.SUPPRESSED_SYSTEM_EXIT_CODE)).isEqualTo("1");
|
||||
}
|
||||
|
||||
private static final class TestLauncher extends Launcher {
|
||||
|
||||
Reference in New Issue
Block a user