[cli-repl][ide-console] Add logging of internal errors

Rename `jvmStatic` to `@JvmStatic`
This commit is contained in:
Dmitry Kovanikov
2015-09-09 19:00:13 +03:00
committed by Pavel V. Talanov
parent 38285496cb
commit 9bf0c8453d
11 changed files with 67 additions and 20 deletions
@@ -126,6 +126,7 @@ public open class K2JVMCompiler : CLICompiler<K2JVMCompilerArguments>() {
configuration.put(JVMConfigurationKeys.MODULE_NAME, arguments.moduleName ?: JvmAbi.DEFAULT_MODULE_NAME)
if (arguments.module == null && arguments.freeArgs.isEmpty() && !arguments.version) {
configuration.put(JVMConfigurationKeys.MODULE_NAME, "replModule")
ReplFromTerminal.run(rootDisposable, configuration)
return ExitCode.OK
}
@@ -26,6 +26,7 @@ import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.jetbrains.kotlin.cli.common.KotlinVersion;
import org.jetbrains.kotlin.cli.jvm.repl.messages.IdeLinebreaksUnescaper;
import org.jetbrains.kotlin.cli.jvm.repl.messages.ReplErrorLogger;
import org.jetbrains.kotlin.cli.jvm.repl.messages.ReplSystemInWrapper;
import org.jetbrains.kotlin.cli.jvm.repl.messages.ReplSystemOutWrapper;
import org.jetbrains.kotlin.config.CompilerConfiguration;
@@ -44,8 +45,9 @@ public class ReplFromTerminal {
private final boolean ideMode;
private ReplSystemInWrapper replReader;
private final ReplSystemOutWrapper replWriter;
private final ReplErrorLogger replErrorLogger;
private final ConsoleReader consoleReader;
private ConsoleReader consoleReader;
public ReplFromTerminal(
@NotNull final Disposable disposable,
@@ -67,6 +69,8 @@ public class ReplFromTerminal {
System.setIn(replReader);
}
replErrorLogger = new ReplErrorLogger(ideMode, replWriter);
new Thread("initialize-repl") {
@Override
public void run() {
@@ -97,7 +101,7 @@ public class ReplFromTerminal {
consoleReader.setHistory(new FileHistory(new File(new File(System.getProperty("user.home")), ".kotlin_history")));
}
catch (Exception e) {
throw UtilsPackage.rethrow(e);
replErrorLogger.logException(e);
}
}
@@ -135,14 +139,14 @@ public class ReplFromTerminal {
}
}
catch (Exception e) {
throw UtilsPackage.rethrow(e);
replErrorLogger.logException(e);
}
finally {
try {
((FileHistory) consoleReader.getHistory()).flush();
}
catch (Exception e) {
replWriter.printlnInnerError("failed to flush history: " + e);
replErrorLogger.logException(e);
}
}
}
@@ -39,10 +39,7 @@ import org.jetbrains.kotlin.cli.jvm.compiler.EnvironmentConfigFiles;
import org.jetbrains.kotlin.cli.jvm.compiler.KotlinCoreEnvironment;
import org.jetbrains.kotlin.cli.jvm.repl.di.ContainerForReplWithJava;
import org.jetbrains.kotlin.cli.jvm.repl.di.DiPackage;
import org.jetbrains.kotlin.cli.jvm.repl.messages.DiagnosticMessageHolder;
import org.jetbrains.kotlin.cli.jvm.repl.messages.ReplIdeDiagnosticMessageHolder;
import org.jetbrains.kotlin.cli.jvm.repl.messages.ReplSystemInWrapper;
import org.jetbrains.kotlin.cli.jvm.repl.messages.ReplTerminalDiagnosticMessageHolder;
import org.jetbrains.kotlin.cli.jvm.repl.messages.*;
import org.jetbrains.kotlin.codegen.ClassBuilderFactories;
import org.jetbrains.kotlin.codegen.CompilationErrorHandler;
import org.jetbrains.kotlin.codegen.KotlinCodegenFacade;
@@ -341,10 +338,10 @@ public class ReplInterpreter {
scriptInstance = scriptInstanceConstructor.newInstance(constructorArgs);
}
catch (Throwable e) {
setReplScriptExecuting(false);
return LineResult.runtimeError(renderStackTrace(e.getCause()));
} finally {
setReplScriptExecuting(false);
}
setReplScriptExecuting(false);
Field rvField = scriptClass.getDeclaredField("rv");
rvField.setAccessible(true);
@@ -387,6 +384,8 @@ public class ReplInterpreter {
}
}
Collections.reverse(newTrace);
// throw away last element which contains Line1.kts<init>(Unknown source)
List<StackTraceElement> resultingTrace = newTrace.subList(0, newTrace.size() - 1);
cause.setStackTrace(resultingTrace.toArray(new StackTraceElement[resultingTrace.size()]));
@@ -0,0 +1,38 @@
/*
* Copyright 2010-2015 JetBrains s.r.o.
*
* 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.jetbrains.kotlin.cli.jvm.repl.messages
import java.io.PrintWriter
import java.io.StringWriter
import kotlin.reflect.jvm.internal.impl.utils.UtilsPackage
public class ReplErrorLogger(private val ideMode: Boolean, private val replWriter: ReplSystemOutWrapper) {
fun logException(e: Throwable?) {
if (ideMode) {
val errorStringWriter = StringWriter()
val errorPrintWriter = PrintWriter(errorStringWriter)
e?.printStackTrace(errorPrintWriter)
val writerString = errorStringWriter.toString()
val internalErrorText = if (writerString.isEmpty()) "Unknown error" else writerString
replWriter.sendInternalErrorReport(internalErrorText)
}
throw UtilsPackage.rethrow(e ?: Throwable("null exception", e))
}
}
@@ -42,7 +42,7 @@ public class ReplSystemInWrapper(
$isReplScriptExecuting = value
}
override synchronized fun read(): Int {
override fun read(): Int {
if (isLastScriptByteProcessed && isReplScriptExecuting) {
isLastScriptByteProcessed = false
return -1
@@ -34,7 +34,7 @@ public class ReplSystemOutWrapper(private val ideMode: Boolean, standardOut: Pri
REPL_INCOMPLETE,
COMPILE_ERROR,
RUNTIME_ERROR,
INNER_ERROR
INTERNAL_ERROR
}
override fun print(x: Boolean) = printWithEscaping(x.toString())
@@ -68,5 +68,5 @@ public class ReplSystemOutWrapper(private val ideMode: Boolean, standardOut: Pri
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)
fun sendInternalErrorReport(x: String) = printlnWithEscaping(x, EscapeType.INTERNAL_ERROR)
}
@@ -38,5 +38,5 @@ fun parseXml(inputMessage: String): String {
}
public object IdeLinebreaksUnescaper {
jvmStatic fun unescapeFromDiez(inputMessage: String) = StringUtil.replace(inputMessage, XML_REPLACEMENTS, SOURCE_CHARS)
@JvmStatic fun unescapeFromDiez(inputMessage: String) = StringUtil.replace(inputMessage, XML_REPLACEMENTS, SOURCE_CHARS)
}