diff --git a/spring-shell-autoconfigure/src/main/java/org/springframework/shell/boot/ExitCodeAutoConfiguration.java b/spring-shell-autoconfigure/src/main/java/org/springframework/shell/boot/ExitCodeAutoConfiguration.java index 3dfec741..cce07949 100644 --- a/spring-shell-autoconfigure/src/main/java/org/springframework/shell/boot/ExitCodeAutoConfiguration.java +++ b/spring-shell-autoconfigure/src/main/java/org/springframework/shell/boot/ExitCodeAutoConfiguration.java @@ -59,7 +59,11 @@ public class ExitCodeAutoConfiguration { @Override public int getExitCode(Throwable exception) { - if (exception.getCause() instanceof CommandExecution.CommandParserExceptionsException) { + // for e vs. its cause, see gh-961 + if (exception instanceof CommandExecution.CommandParserExceptionsException) { + return 2; + } + else if (exception.getCause() instanceof CommandExecution.CommandParserExceptionsException) { return 2; } // only map parsing error so that other mappers can do their job @@ -83,7 +87,11 @@ public class ExitCodeAutoConfiguration { public int getExitCode(Throwable exception) { int exitCode = 0; for (Function function : functions) { - Integer code = function.apply(exception.getCause()); + Throwable cause = exception.getCause(); + if (cause == null) { + cause = exception; + } + Integer code = function.apply(cause); if (code != null) { if (code > 0 && code > exitCode || code < 0 && code < exitCode) { exitCode = code; diff --git a/spring-shell-core/src/main/java/org/springframework/shell/command/CommandRegistration.java b/spring-shell-core/src/main/java/org/springframework/shell/command/CommandRegistration.java index cc3b25f4..2f1b0edc 100644 --- a/spring-shell-core/src/main/java/org/springframework/shell/command/CommandRegistration.java +++ b/spring-shell-core/src/main/java/org/springframework/shell/command/CommandRegistration.java @@ -1080,7 +1080,7 @@ public interface CommandRegistration { @Override public ExitCodeSpec map(Class e, int code) { Function f = t -> { - if (ObjectUtils.nullSafeEquals(t.getClass(), e)) { + if (t != null && ObjectUtils.nullSafeEquals(t.getClass(), e)) { return code; } return 0;