Skip to content

Commit 9779b5c

Browse files
committed
Better errors with non-interactive mode
- Change how errors are printed for interactive vs non-interactive mode. - This now changes behaviour so that stacktrace is printed if non-interactive mode is active so that user has a change to see the full error. - Fixes #427
1 parent b194660 commit 9779b5c

File tree

2 files changed

+47
-14
lines changed

2 files changed

+47
-14
lines changed

spring-shell-core/src/main/java/org/springframework/shell/result/ResultHandlerConfig.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
import org.springframework.context.annotation.Configuration;
2424
import org.springframework.shell.TerminalSizeAware;
2525
import org.springframework.shell.command.CommandCatalog;
26+
import org.springframework.shell.context.ShellContext;
2627
import org.springframework.shell.jline.InteractiveShellRunner;
2728

2829
/**
@@ -62,7 +63,7 @@ public CommandParserExceptionsExceptionResultHandler commandParserExceptionsExce
6263

6364
@Bean
6465
public ThrowableResultHandler throwableResultHandler(Terminal terminal, CommandCatalog commandCatalog,
65-
ObjectProvider<InteractiveShellRunner> interactiveApplicationRunner) {
66-
return new ThrowableResultHandler(terminal, commandCatalog, interactiveApplicationRunner);
66+
ShellContext shellContext, ObjectProvider<InteractiveShellRunner> interactiveApplicationRunner) {
67+
return new ThrowableResultHandler(terminal, commandCatalog, shellContext, interactiveApplicationRunner);
6768
}
6869
}

spring-shell-core/src/main/java/org/springframework/shell/result/ThrowableResultHandler.java

Lines changed: 44 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,11 @@
1313
* See the License for the specific language governing permissions and
1414
* limitations under the License.
1515
*/
16-
1716
package org.springframework.shell.result;
1817

18+
import java.io.PrintWriter;
19+
import java.io.StringWriter;
20+
1921
import org.jline.terminal.Terminal;
2022
import org.jline.utils.AttributedString;
2123
import org.jline.utils.AttributedStringBuilder;
@@ -24,15 +26,20 @@
2426
import org.springframework.beans.factory.ObjectProvider;
2527
import org.springframework.shell.ResultHandler;
2628
import org.springframework.shell.command.CommandCatalog;
29+
import org.springframework.shell.context.InteractionMode;
30+
import org.springframework.shell.context.ShellContext;
2731
import org.springframework.shell.jline.InteractiveShellRunner;
2832
import org.springframework.util.StringUtils;
2933

3034
/**
3135
* A {@link ResultHandler} that prints thrown exceptions messages in red.
3236
*
33-
* <p>Also stores the last exception reported, so that details can be printed using a dedicated command.</p>
37+
* Stores the last exception reported, so that details can be printed using a
38+
* dedicated command if in interactive mode. Prints stacktrace if in
39+
* non-interactive mode as dedicated command could not be used.
3440
*
3541
* @author Eric Bottard
42+
* @author Janne Valkealahti
3643
*/
3744
public class ThrowableResultHandler extends TerminalAwareResultHandler<Throwable> {
3845

@@ -47,30 +54,46 @@ public class ThrowableResultHandler extends TerminalAwareResultHandler<Throwable
4754

4855
private ObjectProvider<InteractiveShellRunner> interactiveRunner;
4956

50-
public ThrowableResultHandler(Terminal terminal, CommandCatalog commandCatalog,
57+
private ShellContext shellContext;
58+
59+
public ThrowableResultHandler(Terminal terminal, CommandCatalog commandCatalog, ShellContext shellContext,
5160
ObjectProvider<InteractiveShellRunner> interactiveRunner) {
5261
super(terminal);
5362
this.commandCatalog = commandCatalog;
63+
this.shellContext = shellContext;
5464
this.interactiveRunner = interactiveRunner;
5565
}
5666

5767
@Override
5868
protected void doHandleResult(Throwable result) {
5969
lastError = result;
60-
String toPrint = StringUtils.hasLength(result.getMessage()) ? result.getMessage() : result.toString();
61-
terminal.writer().println(new AttributedString(toPrint,
70+
boolean shouldHandle = shouldHandle();
71+
72+
if (shouldHandle) {
73+
String errorMsg = StringUtils.hasLength(result.getMessage()) ? result.getMessage() : result.toString();
74+
terminal.writer().println(new AttributedString(errorMsg,
6275
AttributedStyle.DEFAULT.foreground(AttributedStyle.RED)).toAnsi());
63-
if (interactiveRunner.getIfAvailable() != null && commandCatalog.getRegistrations().keySet().contains(DETAILS_COMMAND_NAME)) {
64-
terminal.writer().println(
65-
new AttributedStringBuilder()
76+
77+
String noteMsg;
78+
if (showShortError()) {
79+
noteMsg = new AttributedStringBuilder()
6680
.append("Details of the error have been omitted. You can use the ", AttributedStyle.DEFAULT.foreground(AttributedStyle.RED))
6781
.append(DETAILS_COMMAND_NAME, AttributedStyle.DEFAULT.foreground(AttributedStyle.RED).bold())
6882
.append(" command to print the full stacktrace.", AttributedStyle.DEFAULT.foreground(AttributedStyle.RED))
69-
.toAnsi()
70-
);
83+
.toAnsi();
84+
}
85+
else {
86+
StringWriter sw = new StringWriter();
87+
PrintWriter pw = new PrintWriter(sw);
88+
result.printStackTrace(pw);
89+
String stacktraceStr = sw.toString();
90+
noteMsg = new AttributedString(stacktraceStr,
91+
AttributedStyle.DEFAULT.foreground(AttributedStyle.RED)).toAnsi();
92+
}
93+
terminal.writer().println(noteMsg);
94+
terminal.writer().flush();
7195
}
72-
terminal.writer().flush();
73-
if (interactiveRunner.getIfAvailable() == null) {
96+
else {
7497
if (result instanceof RuntimeException) {
7598
throw (RuntimeException) result;
7699
}
@@ -89,4 +112,13 @@ else if (result instanceof Error) {
89112
public Throwable getLastError() {
90113
return lastError;
91114
}
115+
116+
private boolean shouldHandle() {
117+
return interactiveRunner.getIfAvailable() != null;
118+
}
119+
120+
private boolean showShortError() {
121+
return commandCatalog.getRegistrations().keySet().contains(DETAILS_COMMAND_NAME)
122+
&& this.shellContext.getInteractionMode() == InteractionMode.INTERACTIVE;
123+
}
92124
}

0 commit comments

Comments
 (0)