Use getMessage() by default when printing exceptions.

Fixes #176
This commit is contained in:
Eric Bottard
2017-11-06 15:03:05 +01:00
parent 7d50c0813f
commit e2c8be2b23
2 changed files with 5 additions and 44 deletions

View File

@@ -1,43 +0,0 @@
/*
* Copyright 2017 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.shell.result;
import org.jline.utils.AttributedString;
import org.jline.utils.AttributedStyle;
import org.springframework.shell.CommandNotFound;
import org.springframework.shell.ResultHandler;
import org.springframework.stereotype.Component;
/**
* Used when no command can be matched for user input.
*
* Simply prints an error message, without printing the exception class.
*
* @author Eric Bottard
*/
@Component
public class CommandNotFoundResultHandler extends TerminalAwareResultHandler<CommandNotFound> {
@Override
protected void doHandleResult(CommandNotFound result) {
terminal.writer().println(new AttributedString(result.getMessage(),
AttributedStyle.DEFAULT.foreground(AttributedStyle.RED)).toAnsi());
terminal.writer().flush();
}
}

View File

@@ -25,10 +25,13 @@ import org.springframework.context.annotation.Lazy;
import org.springframework.shell.CommandRegistry;
import org.springframework.shell.ResultHandler;
import org.springframework.stereotype.Component;
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>
*
* @author Eric Bottard
*/
@Component
@@ -47,7 +50,8 @@ public class ThrowableResultHandler extends TerminalAwareResultHandler<Throwable
@Override
protected void doHandleResult(Throwable result) {
lastError = result;
terminal.writer().println(new AttributedString(result.toString(),
String toPrint = StringUtils.hasLength(result.getMessage()) ? result.getMessage() : result.toString();
terminal.writer().println(new AttributedString(toPrint,
AttributedStyle.DEFAULT.foreground(AttributedStyle.RED)).toAnsi());
if (commandRegistry.listCommands().containsKey(DETAILS_COMMAND_NAME)) {
terminal.writer().println(