13
13
* See the License for the specific language governing permissions and
14
14
* limitations under the License.
15
15
*/
16
-
17
16
package org .springframework .shell .result ;
18
17
18
+ import java .io .PrintWriter ;
19
+ import java .io .StringWriter ;
20
+
19
21
import org .jline .terminal .Terminal ;
20
22
import org .jline .utils .AttributedString ;
21
23
import org .jline .utils .AttributedStringBuilder ;
24
26
import org .springframework .beans .factory .ObjectProvider ;
25
27
import org .springframework .shell .ResultHandler ;
26
28
import org .springframework .shell .command .CommandCatalog ;
29
+ import org .springframework .shell .context .InteractionMode ;
30
+ import org .springframework .shell .context .ShellContext ;
27
31
import org .springframework .shell .jline .InteractiveShellRunner ;
28
32
import org .springframework .util .StringUtils ;
29
33
30
34
/**
31
35
* A {@link ResultHandler} that prints thrown exceptions messages in red.
32
36
*
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.
34
40
*
35
41
* @author Eric Bottard
42
+ * @author Janne Valkealahti
36
43
*/
37
44
public class ThrowableResultHandler extends TerminalAwareResultHandler <Throwable > {
38
45
@@ -47,30 +54,46 @@ public class ThrowableResultHandler extends TerminalAwareResultHandler<Throwable
47
54
48
55
private ObjectProvider <InteractiveShellRunner > interactiveRunner ;
49
56
50
- public ThrowableResultHandler (Terminal terminal , CommandCatalog commandCatalog ,
57
+ private ShellContext shellContext ;
58
+
59
+ public ThrowableResultHandler (Terminal terminal , CommandCatalog commandCatalog , ShellContext shellContext ,
51
60
ObjectProvider <InteractiveShellRunner > interactiveRunner ) {
52
61
super (terminal );
53
62
this .commandCatalog = commandCatalog ;
63
+ this .shellContext = shellContext ;
54
64
this .interactiveRunner = interactiveRunner ;
55
65
}
56
66
57
67
@ Override
58
68
protected void doHandleResult (Throwable result ) {
59
69
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 ,
62
75
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 ()
66
80
.append ("Details of the error have been omitted. You can use the " , AttributedStyle .DEFAULT .foreground (AttributedStyle .RED ))
67
81
.append (DETAILS_COMMAND_NAME , AttributedStyle .DEFAULT .foreground (AttributedStyle .RED ).bold ())
68
82
.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 ();
71
95
}
72
- terminal .writer ().flush ();
73
- if (interactiveRunner .getIfAvailable () == null ) {
96
+ else {
74
97
if (result instanceof RuntimeException ) {
75
98
throw (RuntimeException ) result ;
76
99
}
@@ -89,4 +112,13 @@ else if (result instanceof Error) {
89
112
public Throwable getLastError () {
90
113
return lastError ;
91
114
}
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
+ }
92
124
}
0 commit comments