Skip to content

Commit 8a52662

Browse files
committed
Fix exit code in non-interactive shell
Resolves #1339
1 parent fd656b6 commit 8a52662

2 files changed

Lines changed: 37 additions & 1 deletion

File tree

spring-shell-core/src/main/java/org/springframework/shell/core/NonInteractiveShellRunner.java

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
import org.apache.commons.logging.LogFactory;
2424

2525
import org.springframework.shell.core.command.CommandContext;
26+
import org.springframework.shell.core.command.CommandExecutionException;
2627
import org.springframework.shell.core.command.CommandExecutor;
2728
import org.springframework.shell.core.command.CommandParser;
2829
import org.springframework.shell.core.command.CommandRegistry;
@@ -137,7 +138,8 @@ private void executeScript(InputProvider inputProvider) {
137138
if (ExitStatus.OK.code() != exitStatus.code()) { // business error
138139
log.error("Command " + parsedInput.commandName() + " returned an error: " + exitStatus.description()
139140
+ ". Skipping next commands in the script");
140-
break;
141+
throw new CommandExecutionException("Unable to execute command " + parsedInput.commandName() + ": "
142+
+ exitStatus.description() + ". Skipping next commands in the script");
141143
}
142144
}
143145
}
@@ -156,6 +158,8 @@ private void executeCommand(String primaryCommand) {
156158
ExitStatus exitStatus = this.commandExecutor.execute(commandContext);
157159
if (ExitStatus.OK.code() != exitStatus.code()) {
158160
log.error("Command " + parsedInput.commandName() + " returned an error: " + exitStatus.description());
161+
throw new CommandExecutionException(
162+
"Unable to execute command " + primaryCommand + ": " + exitStatus.description(), exitStatus.code());
159163
}
160164
}
161165

spring-shell-core/src/main/java/org/springframework/shell/core/command/CommandExecutionException.java

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,12 +24,44 @@
2424
*/
2525
public class CommandExecutionException extends RuntimeException {
2626

27+
private int exitCode = ExitStatus.EXECUTION_ERROR.code();
28+
29+
/**
30+
* Create a new {@code CommandExecutionException} with the given message.
31+
* @param message the detail message
32+
*/
2733
public CommandExecutionException(String message) {
2834
super(message);
2935
}
3036

37+
/**
38+
* Create a new {@code CommandExecutionException} with the given message and exit
39+
* code.
40+
* @param message the detail message
41+
* @param exitCode the exit code associated with this exception
42+
* @since 4.0.2
43+
*/
44+
public CommandExecutionException(String message, int exitCode) {
45+
super(message);
46+
this.exitCode = exitCode;
47+
}
48+
49+
/**
50+
* Create a new {@code CommandExecutionException} with the given message and cause.
51+
* @param message the detail message
52+
* @param cause the cause
53+
*/
3154
public CommandExecutionException(String message, Throwable cause) {
3255
super(message, cause);
3356
}
3457

58+
/**
59+
* Return the exit code associated with this exception.
60+
* @return the exit code
61+
* @since 4.0.2
62+
*/
63+
public int getExitCode() {
64+
return this.exitCode;
65+
}
66+
3567
}

0 commit comments

Comments
 (0)