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.
This commit is contained in:
@@ -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
|
||||
}
|
||||
}
|
||||
@@ -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)
|
||||
}
|
||||
}
|
||||
@@ -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
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user