[cli-repl][ide-console] Show runtime errors in IDE console

This commit is contained in:
Dmitry Kovanikov
2015-08-24 16:31:35 +03:00
committed by Pavel V. Talanov
parent e23441907c
commit a17363459a
6 changed files with 73 additions and 39 deletions
@@ -134,7 +134,7 @@ public class ReplFromTerminal {
((FileHistory) consoleReader.getHistory()).flush();
}
catch (Exception e) {
System.err.println("failed to flush history: " + e);
replWriter.printlnInnerError("failed to flush history: " + e);
}
}
}
@@ -203,8 +203,11 @@ public class ReplFromTerminal {
else if (lineResult.getType() == ReplInterpreter.LineResultType.INCOMPLETE) {
replWriter.printlnIncomplete();
}
else if (lineResult.getType() == ReplInterpreter.LineResultType.ERROR) {
replWriter.printlnError(lineResult.getErrorText());
else if (lineResult.getType() == ReplInterpreter.LineResultType.COMPILE_ERROR) {
replWriter.printlnCompileError(lineResult.getErrorText());
}
else if (lineResult.getType() == ReplInterpreter.LineResultType.RUNTIME_ERROR) {
replWriter.printlnRuntimeError(lineResult.getErrorText());
}
else {
throw new IllegalStateException("unknown line result type: " + lineResult);
@@ -166,7 +166,8 @@ public class ReplInterpreter {
public enum LineResultType {
SUCCESS,
ERROR,
COMPILE_ERROR,
RUNTIME_ERROR,
INCOMPLETE,
}
@@ -209,11 +210,8 @@ public class ReplInterpreter {
return errorText;
}
public static LineResult successful(Object value, boolean unit) {
return new LineResult(value, unit, null, LineResultType.SUCCESS);
}
public static LineResult error(@NotNull String errorText) {
@NotNull
private static LineResult error(@NotNull String errorText, @NotNull LineResultType errorType) {
if (errorText.isEmpty()) {
errorText = "<unknown error>";
}
@@ -221,7 +219,22 @@ public class ReplInterpreter {
errorText += "\n";
}
return new LineResult(null, false, errorText, LineResultType.ERROR);
return new LineResult(null, false, errorText, errorType);
}
@NotNull
public static LineResult successful(Object value, boolean unit) {
return new LineResult(value, unit, null, LineResultType.SUCCESS);
}
@NotNull
public static LineResult compileError(@NotNull String errorText) {
return error(errorText, LineResultType.COMPILE_ERROR);
}
@NotNull
public static LineResult runtimeError(@NotNull String errorText) {
return error(errorText, LineResultType.RUNTIME_ERROR);
}
public static LineResult incomplete() {
@@ -265,7 +278,7 @@ public class ReplInterpreter {
previousIncompleteLines.clear();
if (syntaxErrorReport.isHasErrors()) {
return LineResult.error(errorHolder.getRenderedDiagnostics());
return LineResult.compileError(errorHolder.getRenderedDiagnostics());
}
prepareForTheNextReplLine(topDownAnalysisContext);
@@ -276,7 +289,7 @@ public class ReplInterpreter {
ScriptDescriptor scriptDescriptor = doAnalyze(psiFile, errorHolder);
if (scriptDescriptor == null) {
return LineResult.error(errorHolder.getRenderedDiagnostics());
return LineResult.compileError(errorHolder.getRenderedDiagnostics());
}
List<Pair<ScriptDescriptor, Type>> earlierScripts = Lists.newArrayList();
@@ -314,7 +327,7 @@ public class ReplInterpreter {
scriptInstance = scriptInstanceConstructor.newInstance(constructorArgs);
}
catch (Throwable e) {
return LineResult.error(renderStackTrace(e.getCause()));
return LineResult.runtimeError(renderStackTrace(e.getCause()));
}
Field rvField = scriptClass.getDeclaredField("rv");
rvField.setAccessible(true);
@@ -351,7 +364,9 @@ public class ReplInterpreter {
}
}
Collections.reverse(newTrace);
cause.setStackTrace(newTrace.toArray(new StackTraceElement[newTrace.size()]));
List<StackTraceElement> resultingTrace = newTrace.subList(0, newTrace.size() - 1);
cause.setStackTrace(resultingTrace.toArray(new StackTraceElement[resultingTrace.size()]));
return Throwables.getStackTraceAsString(cause);
}
@@ -28,7 +28,9 @@ public class ReplSystemOutWrapper(private val standardOut: PrintStream, private
USER_OUTPUT,
REPL_RESULT,
REPL_INCOMPLETE,
ERROR
COMPILE_ERROR,
RUNTIME_ERROR,
INNER_ERROR
}
override fun print(x: Boolean) = printWithEscaping(x.toString())
@@ -54,8 +56,10 @@ public class ReplSystemOutWrapper(private val standardOut: PrintStream, private
return "${EscapeConstants.XML_PREAMBLE}<output type=\"$escapeType\">${StringUtil.escapeXml(singleLine)}</output>"
}
fun printlnResult(x: Any?) = printlnWithEscaping(x.toString(), EscapeType.REPL_RESULT)
fun printlnError(x: String) = printlnWithEscaping(x, EscapeType.ERROR)
fun printlnInit(x: String) = printlnWithEscaping(x, EscapeType.INITIAL_PROMPT)
fun printlnResult(x: Any?) = printlnWithEscaping(x.toString(), EscapeType.REPL_RESULT)
fun printlnIncomplete() = printlnWithEscaping("", EscapeType.REPL_INCOMPLETE)
fun printlnCompileError(x: String) = printlnWithEscaping(x, EscapeType.COMPILE_ERROR)
fun printlnRuntimeError(x: String) = printlnWithEscaping(x, EscapeType.RUNTIME_ERROR)
fun printlnInnerError(x: String) = printlnWithEscaping(x, EscapeType.INNER_ERROR)
}