Skip to content

Commit aebf6f4

Browse files
committed
Fix #3305: Handle exceptions in REPL
The new REPL did not catch exceptions. We now handle exceptions but there is a lot of room for improvement when reporting an exception.
1 parent 35a3d8d commit aebf6f4

File tree

1 file changed

+23
-5
lines changed

1 file changed

+23
-5
lines changed

compiler/src/dotty/tools/repl/Rendering.scala

Lines changed: 23 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
package dotty.tools
22
package repl
33

4-
import java.lang.ClassLoader
4+
import java.io.{ StringWriter, PrintWriter }
5+
import java.lang.{ ClassLoader, ExceptionInInitializerError }
6+
import java.lang.reflect.InvocationTargetException
57

68
import scala.util.control.NonFatal
79

@@ -70,10 +72,26 @@ private[repl] class Rendering(compiler: ReplCompiler,
7072
/** Render value definition result */
7173
def renderVal(d: Denotation)(implicit ctx: Context): Option[String] = {
7274
val dcl = d.symbol.showUser
73-
val resultValue =
74-
if (d.symbol.is(Flags.Lazy)) Some("<lazy>")
75-
else valueOf(d.symbol)
7675

77-
resultValue.map(value => s"$dcl = $value")
76+
try {
77+
val resultValue =
78+
if (d.symbol.is(Flags.Lazy)) Some("<lazy>")
79+
else valueOf(d.symbol)
80+
81+
resultValue.map(value => s"$dcl = $value")
82+
}
83+
catch { case ex: InvocationTargetException => Some(renderError(ex)) }
84+
}
85+
86+
/** Render the stack trace of the underlying exception */
87+
private def renderError(ex: InvocationTargetException): String = {
88+
val cause = ex.getCause match {
89+
case ex: ExceptionInInitializerError => ex.getCause
90+
case ex => ex
91+
}
92+
val sw = new StringWriter()
93+
val pw = new PrintWriter(sw)
94+
cause.printStackTrace(pw)
95+
sw.toString
7896
}
7997
}

0 commit comments

Comments
 (0)