Skip to content

Better errors with non-interactive mode #428

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
May 27, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import org.springframework.context.annotation.Configuration;
import org.springframework.shell.TerminalSizeAware;
import org.springframework.shell.command.CommandCatalog;
import org.springframework.shell.context.ShellContext;
import org.springframework.shell.jline.InteractiveShellRunner;

/**
Expand Down Expand Up @@ -62,7 +63,7 @@ public CommandParserExceptionsExceptionResultHandler commandParserExceptionsExce

@Bean
public ThrowableResultHandler throwableResultHandler(Terminal terminal, CommandCatalog commandCatalog,
ObjectProvider<InteractiveShellRunner> interactiveApplicationRunner) {
return new ThrowableResultHandler(terminal, commandCatalog, interactiveApplicationRunner);
ShellContext shellContext, ObjectProvider<InteractiveShellRunner> interactiveApplicationRunner) {
return new ThrowableResultHandler(terminal, commandCatalog, shellContext, interactiveApplicationRunner);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,11 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package org.springframework.shell.result;

import java.io.PrintWriter;
import java.io.StringWriter;

import org.jline.terminal.Terminal;
import org.jline.utils.AttributedString;
import org.jline.utils.AttributedStringBuilder;
Expand All @@ -24,15 +26,20 @@
import org.springframework.beans.factory.ObjectProvider;
import org.springframework.shell.ResultHandler;
import org.springframework.shell.command.CommandCatalog;
import org.springframework.shell.context.InteractionMode;
import org.springframework.shell.context.ShellContext;
import org.springframework.shell.jline.InteractiveShellRunner;
import org.springframework.util.StringUtils;

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

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

private ObjectProvider<InteractiveShellRunner> interactiveRunner;

public ThrowableResultHandler(Terminal terminal, CommandCatalog commandCatalog,
private ShellContext shellContext;

public ThrowableResultHandler(Terminal terminal, CommandCatalog commandCatalog, ShellContext shellContext,
ObjectProvider<InteractiveShellRunner> interactiveRunner) {
super(terminal);
this.commandCatalog = commandCatalog;
this.shellContext = shellContext;
this.interactiveRunner = interactiveRunner;
}

@Override
protected void doHandleResult(Throwable result) {
lastError = result;
String toPrint = StringUtils.hasLength(result.getMessage()) ? result.getMessage() : result.toString();
terminal.writer().println(new AttributedString(toPrint,
boolean shouldHandle = shouldHandle();

if (shouldHandle) {
String errorMsg = StringUtils.hasLength(result.getMessage()) ? result.getMessage() : result.toString();
terminal.writer().println(new AttributedString(errorMsg,
AttributedStyle.DEFAULT.foreground(AttributedStyle.RED)).toAnsi());
if (interactiveRunner.getIfAvailable() != null && commandCatalog.getRegistrations().keySet().contains(DETAILS_COMMAND_NAME)) {
terminal.writer().println(
new AttributedStringBuilder()

String noteMsg;
if (showShortError()) {
noteMsg = new AttributedStringBuilder()
.append("Details of the error have been omitted. You can use the ", AttributedStyle.DEFAULT.foreground(AttributedStyle.RED))
.append(DETAILS_COMMAND_NAME, AttributedStyle.DEFAULT.foreground(AttributedStyle.RED).bold())
.append(" command to print the full stacktrace.", AttributedStyle.DEFAULT.foreground(AttributedStyle.RED))
.toAnsi()
);
.toAnsi();
}
else {
StringWriter sw = new StringWriter();
PrintWriter pw = new PrintWriter(sw);
result.printStackTrace(pw);
String stacktraceStr = sw.toString();
noteMsg = new AttributedString(stacktraceStr,
AttributedStyle.DEFAULT.foreground(AttributedStyle.RED)).toAnsi();
}
terminal.writer().println(noteMsg);
terminal.writer().flush();
}
terminal.writer().flush();
if (interactiveRunner.getIfAvailable() == null) {
else {
if (result instanceof RuntimeException) {
throw (RuntimeException) result;
}
Expand All @@ -89,4 +112,13 @@ else if (result instanceof Error) {
public Throwable getLastError() {
return lastError;
}

private boolean shouldHandle() {
return interactiveRunner.getIfAvailable() != null;
}

private boolean showShortError() {
return commandCatalog.getRegistrations().keySet().contains(DETAILS_COMMAND_NAME)
&& this.shellContext.getInteractionMode() == InteractionMode.INTERACTIVE;
}
}