Skip to content

Commit 74ccdfb

Browse files
committed
Print a simple error message when a tools command fails
ToolsJarMode wrapped every exception in IllegalStateException, so a JarModeErrorException thrown by a command never reached the simple message branch of JarModeRunner and was printed as a stack trace. Rethrow it so failures such as a missing SBOM are reported as 'Error: <message>' with a non-zero exit code. Signed-off-by: Hyeongjun Cho <ryuu.public@gmail.com>
1 parent 28ecd8f commit 74ccdfb

2 files changed

Lines changed: 14 additions & 0 deletions

File tree

loader/spring-boot-jarmode-tools/src/main/java/org/springframework/boot/jarmode/tools/ToolsJarMode.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,11 +22,13 @@
2222
import org.jspecify.annotations.Nullable;
2323

2424
import org.springframework.boot.loader.jarmode.JarMode;
25+
import org.springframework.boot.loader.jarmode.JarModeErrorException;
2526

2627
/**
2728
* {@link JarMode} providing {@code "tools"} support.
2829
*
2930
* @author Moritz Halbritter
31+
* @author Hyeongjun Cho
3032
* @since 3.3.0
3133
*/
3234
public class ToolsJarMode implements JarMode {
@@ -54,6 +56,9 @@ public void run(String mode, String[] args) {
5456
try {
5557
new Runner(this.out, this.context, getCommands(this.context)).run(args);
5658
}
59+
catch (JarModeErrorException ex) {
60+
throw ex;
61+
}
5762
catch (Exception ex) {
5863
throw new IllegalStateException(ex);
5964
}

loader/spring-boot-jarmode-tools/src/test/java/org/springframework/boot/jarmode/tools/ToolsJarModeTests.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,12 +21,16 @@
2121
import org.junit.jupiter.api.BeforeEach;
2222
import org.junit.jupiter.api.Test;
2323

24+
import org.springframework.boot.loader.jarmode.JarModeErrorException;
25+
2426
import static org.assertj.core.api.Assertions.assertThat;
27+
import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
2528

2629
/**
2730
* Tests for {@link ToolsJarMode}.
2831
*
2932
* @author Moritz Halbritter
33+
* @author Hyeongjun Cho
3034
*/
3135
class ToolsJarModeTests extends AbstractJarModeTests {
3236

@@ -101,6 +105,11 @@ void optionMissingRequiredValueShowsErrorAndCommandHelp() {
101105
assertThat(this.out).hasSameContentAsResource("tools-error-option-missing-value-output.txt");
102106
}
103107

108+
@Test
109+
void commandFailureIsThrownAsJarModeErrorException() {
110+
assertThatExceptionOfType(JarModeErrorException.class).isThrownBy(() -> run("sbom"));
111+
}
112+
104113
private void run(String... args) {
105114
this.mode.run("tools", args);
106115
}

0 commit comments

Comments
 (0)