Skip to content

Commit 51f49f7

Browse files
committed
Fix exception mapping with exit codes
- Migitates changed behaviour in boot how exceptions are handled thus caused trouble with exit code mappings on a shell side. - Fixes #961
1 parent cb8f44f commit 51f49f7

File tree

2 files changed

+11
-3
lines changed

2 files changed

+11
-3
lines changed

spring-shell-autoconfigure/src/main/java/org/springframework/shell/boot/ExitCodeAutoConfiguration.java

+10-2
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,11 @@ static class ShellExitCodeExceptionMapper implements ExitCodeExceptionMapper {
5959

6060
@Override
6161
public int getExitCode(Throwable exception) {
62-
if (exception.getCause() instanceof CommandExecution.CommandParserExceptionsException) {
62+
// for e vs. its cause, see gh-961
63+
if (exception instanceof CommandExecution.CommandParserExceptionsException) {
64+
return 2;
65+
}
66+
else if (exception.getCause() instanceof CommandExecution.CommandParserExceptionsException) {
6367
return 2;
6468
}
6569
// only map parsing error so that other mappers can do their job
@@ -83,7 +87,11 @@ public void reset(List<Function<Throwable, Integer>> functions) {
8387
public int getExitCode(Throwable exception) {
8488
int exitCode = 0;
8589
for (Function<Throwable, Integer> function : functions) {
86-
Integer code = function.apply(exception.getCause());
90+
Throwable cause = exception.getCause();
91+
if (cause == null) {
92+
cause = exception;
93+
}
94+
Integer code = function.apply(cause);
8795
if (code != null) {
8896
if (code > 0 && code > exitCode || code < 0 && code < exitCode) {
8997
exitCode = code;

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -1080,7 +1080,7 @@ static class DefaultExitCodeSpec implements ExitCodeSpec {
10801080
@Override
10811081
public ExitCodeSpec map(Class<? extends Throwable> e, int code) {
10821082
Function<Throwable, Integer> f = t -> {
1083-
if (ObjectUtils.nullSafeEquals(t.getClass(), e)) {
1083+
if (t != null && ObjectUtils.nullSafeEquals(t.getClass(), e)) {
10841084
return code;
10851085
}
10861086
return 0;

0 commit comments

Comments
 (0)