fix: Catch gson parsing error

If non-JSON leaks into stdout, send it to the logger.
This commit is contained in:
Eugene Yokota 2025-09-13 22:52:24 -04:00
parent 2db398aa47
commit f7a3c593e9
1 changed files with 15 additions and 11 deletions

View File

@ -8,7 +8,7 @@
package sbt
import com.google.gson.{ JsonObject, JsonParser }
import com.google.gson.{ JsonObject, JsonParser, JsonSyntaxException }
import testing.{ Logger as _, Task as _, * }
import java.io.*
import java.util.ArrayList
@ -25,6 +25,7 @@ import scala.collection.mutable
import scala.concurrent.{ Await, Promise }
import scala.concurrent.duration.Duration
import scala.util.Random
import scala.util.control.NonFatal
import scala.jdk.CollectionConverters.*
import scala.sys.process.Process
@ -186,17 +187,20 @@ private class React(
val g = WorkerMain.mkGson()
val promise: Promise[Int] = Promise()
override def apply(line: String): Unit =
// scala.Console.err.println(line)
val o = JsonParser.parseString(line).getAsJsonObject()
if o.has("id") then
val resId = o.getAsJsonPrimitive("id").getAsLong()
if resId == id then
if promise.isCompleted then ()
else if o.has("error") then promise.failure(new RuntimeException(line))
else promise.success(0)
try
val o = JsonParser.parseString(line).getAsJsonObject()
if o.has("id") then
val resId = o.getAsJsonPrimitive("id").getAsLong()
if resId == id then
if promise.isCompleted then ()
else if o.has("error") then promise.failure(new RuntimeException(line))
else promise.success(0)
else ()
else if o.has("method") then processNotification(o)
else ()
else if o.has("method") then processNotification(o)
else ()
catch
case _: JsonSyntaxException => log.info(line)
case NonFatal(_) => ()
override def notifyExit(p: Process): Unit =
if !process.isAlive then promise.success(process.exitValue())