From 54251ddac002ea3bdccfafe7c532cc2d964a6baf Mon Sep 17 00:00:00 2001 From: Yan Zhulanow Date: Fri, 1 Dec 2017 17:50:40 +0300 Subject: [PATCH] REPL, Refactoring: Rename ReplErrorLogger to ReplExceptionReporter ReplErrorLogger is not really a class that logs errors, ReplWriter exists for that purpose (I don't think ReplWriter has a good API as well, though). The only thing it does is that is passes exceptions got from the compiler to IDE. So, in my opinion, the old name is rather confusing and it doesn't describe what the class actually do. Also rethrow exceptions explicitly. --- .../kotlin/cli/jvm/repl/ReplConfiguration.kt | 35 +++------------- .../cli/jvm/repl/ReplExceptionReporter.kt | 42 +++++++++++++++++++ .../kotlin/cli/jvm/repl/ReplFromTerminal.kt | 9 ++-- 3 files changed, 53 insertions(+), 33 deletions(-) create mode 100644 compiler/cli/src/org/jetbrains/kotlin/cli/jvm/repl/ReplExceptionReporter.kt diff --git a/compiler/cli/src/org/jetbrains/kotlin/cli/jvm/repl/ReplConfiguration.kt b/compiler/cli/src/org/jetbrains/kotlin/cli/jvm/repl/ReplConfiguration.kt index c6406754ce5..dd917885a80 100644 --- a/compiler/cli/src/org/jetbrains/kotlin/cli/jvm/repl/ReplConfiguration.kt +++ b/compiler/cli/src/org/jetbrains/kotlin/cli/jvm/repl/ReplConfiguration.kt @@ -20,12 +20,10 @@ import org.jetbrains.kotlin.cli.jvm.repl.messages.* import org.jetbrains.kotlin.cli.jvm.repl.reader.ConsoleReplCommandReader import org.jetbrains.kotlin.cli.jvm.repl.reader.IdeReplCommandReader import org.jetbrains.kotlin.cli.jvm.repl.reader.ReplCommandReader -import java.io.PrintWriter -import java.io.StringWriter interface ReplConfiguration { val writer: ReplWriter - val errorLogger: ReplErrorLogger + val exceptionReporter: ReplExceptionReporter val commandReader: ReplCommandReader val allowIncompleteLines: Boolean @@ -44,7 +42,7 @@ class ReplForIdeConfiguration : ReplConfiguration { override fun createDiagnosticHolder() = ReplIdeDiagnosticMessageHolder() override val writer: ReplWriter - override val errorLogger: ReplErrorLogger + override val exceptionReporter: ReplExceptionReporter override val commandReader: ReplCommandReader val sinWrapper: ReplSystemInWrapper @@ -62,7 +60,7 @@ class ReplForIdeConfiguration : ReplConfiguration { System.setIn(sinWrapper) writer = soutWrapper - errorLogger = IdeReplErrorLogger(writer) + exceptionReporter = IdeReplExceptionReporter(writer) commandReader = IdeReplCommandReader() } } @@ -78,35 +76,12 @@ class ConsoleReplConfiguration : ReplConfiguration { override fun createDiagnosticHolder() = ReplTerminalDiagnosticMessageHolder() override val writer: ReplWriter - override val errorLogger: ReplErrorLogger + override val exceptionReporter: ReplExceptionReporter override val commandReader: ReplCommandReader init { writer = ReplConsoleWriter() - errorLogger = object : ReplErrorLogger { - override fun logException(e: Throwable): Nothing { - throw e - } - } - + exceptionReporter = ReplExceptionReporter.DoNothing commandReader = ConsoleReplCommandReader() } -} - -interface ReplErrorLogger { - fun logException(e: Throwable): Nothing -} - -class IdeReplErrorLogger(private val replWriter: ReplWriter) : ReplErrorLogger { - override fun logException(e: Throwable): Nothing { - 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 e - } } \ No newline at end of file diff --git a/compiler/cli/src/org/jetbrains/kotlin/cli/jvm/repl/ReplExceptionReporter.kt b/compiler/cli/src/org/jetbrains/kotlin/cli/jvm/repl/ReplExceptionReporter.kt new file mode 100644 index 00000000000..3c842152df8 --- /dev/null +++ b/compiler/cli/src/org/jetbrains/kotlin/cli/jvm/repl/ReplExceptionReporter.kt @@ -0,0 +1,42 @@ +/* + * Copyright 2010-2017 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 + +import org.jetbrains.kotlin.cli.jvm.repl.messages.ReplWriter +import java.io.PrintWriter +import java.io.StringWriter + +interface ReplExceptionReporter { + fun report(e: Throwable) + + companion object DoNothing : ReplExceptionReporter { + override fun report(e: Throwable) {} + } +} + +class IdeReplExceptionReporter(private val replWriter: ReplWriter) : ReplExceptionReporter { + override fun report(e: Throwable) { + val stringWriter = StringWriter() + val printWriter = PrintWriter(stringWriter) + e.printStackTrace(printWriter) + + val writerString = stringWriter.toString() + val internalErrorText = if (writerString.isEmpty()) "Unknown error" else writerString + + replWriter.sendInternalErrorReport(internalErrorText) + } +} \ No newline at end of file diff --git a/compiler/cli/src/org/jetbrains/kotlin/cli/jvm/repl/ReplFromTerminal.kt b/compiler/cli/src/org/jetbrains/kotlin/cli/jvm/repl/ReplFromTerminal.kt index b4214d49780..d50c5f70929 100644 --- a/compiler/cli/src/org/jetbrains/kotlin/cli/jvm/repl/ReplFromTerminal.kt +++ b/compiler/cli/src/org/jetbrains/kotlin/cli/jvm/repl/ReplFromTerminal.kt @@ -67,14 +67,16 @@ class ReplFromTerminal( } } catch (e: Exception) { - errorLogger.logException(e) + exceptionReporter.report(e) + throw e } finally { try { commandReader.flushHistory() } catch (e: Exception) { - errorLogger.logException(e) + exceptionReporter.report(e) + throw e } } @@ -167,7 +169,8 @@ class ReplFromTerminal( ReplFromTerminal(disposable, configuration, replConfiguration).doRun() } catch (e: Exception) { - replConfiguration.errorLogger.logException(e) + replConfiguration.exceptionReporter.report(e) + throw e } } }