Skip to content

Commit

Permalink
Fix run, add better logging for failed runs
Browse files Browse the repository at this point in the history
  • Loading branch information
oyvindberg committed Jul 1, 2022
1 parent a84abb4 commit c7083d7
Show file tree
Hide file tree
Showing 5 changed files with 49 additions and 15 deletions.
2 changes: 1 addition & 1 deletion bleep-core/src/main/scala/bleep/BleepCommandRemote.scala
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ abstract class BleepCommandRemote(started: Started) extends BleepCommand {
buildClient.failed match {
case empty if empty.isEmpty => Right(())
case failed =>
Left(new BspCommandFailed("Failed", failed.map(projectFromBuildTarget).toList, bsp4j.StatusCode.ERROR))
Left(new BspCommandFailed("Failed", failed.map(projectFromBuildTarget).toList, BspCommandFailed.NoDetails))
}
}
finally
Expand Down
30 changes: 22 additions & 8 deletions bleep-core/src/main/scala/bleep/bsp/BspCommandFailed.scala
Original file line number Diff line number Diff line change
@@ -1,13 +1,27 @@
package bleep.bsp

import bleep.{model, BuildException}
import org.eclipse.lsp4j.jsonrpc.messages.ResponseError
import ch.epfl.scala.bsp4j

class BspCommandFailed(what: String, projects: List[model.CrossProjectName], code: bsp4j.StatusCode)
extends BuildException(
code match {
case bsp4j.StatusCode.OK => sys.error("unexpected")
case bsp4j.StatusCode.ERROR => s"$what ${projects.map(_.value).mkString(", ")} failed"
case bsp4j.StatusCode.CANCELLED => s"$what ${projects.map(_.value).mkString(", ")} was cancelled"
}
)
class BspCommandFailed(what: String, projects: List[model.CrossProjectName], reason: BspCommandFailed.Reason)
extends BuildException(s"$what ${projects.map(_.value).mkString(", ")} ${reason.str}", reason.throwable.orNull)

object BspCommandFailed {
sealed trait Reason {
def str: String = this match {
case NoDetails => ""
case StatusCode(code) => s"failed status code $code"
case FoundResponseError(value) => s"failed with response error $value"
case FailedWithException(value) => s"failed with exception: ${value.getMessage}"
}
def throwable: Option[Throwable] = this match {
case FailedWithException(value) => Some(value)
case _ => None
}
}
case object NoDetails extends Reason
case class StatusCode(code: bsp4j.StatusCode) extends Reason
case class FoundResponseError(value: ResponseError) extends Reason
case class FailedWithException(value: Throwable) extends Reason
}
2 changes: 1 addition & 1 deletion bleep-core/src/main/scala/bleep/commands/Compile.scala
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ case class Compile(started: Started, projects: List[model.CrossProjectName]) ext

result.getStatusCode match {
case bsp4j.StatusCode.OK => Right(started.logger.info("Compilation succeeded"))
case other => Left(new BspCommandFailed(s"compile", projects, other))
case other => Left(new BspCommandFailed(s"compile", projects, BspCommandFailed.StatusCode(other)))
}
}
}
26 changes: 23 additions & 3 deletions bleep-core/src/main/scala/bleep/commands/Run.scala
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,12 @@ package commands
import bleep.bsp.BspCommandFailed
import ch.epfl.scala.bsp4j
import ch.epfl.scala.bsp4j.{RunParams, ScalaMainClass, ScalaMainClassesParams, ScalaMainClassesResult}
import org.eclipse.lsp4j.jsonrpc.messages.ResponseError

import java.util
import scala.build.bloop.BloopServer
import scala.jdk.CollectionConverters._
import scala.util.{Failure, Success, Try}

case class Run(
started: Started,
Expand All @@ -30,13 +32,31 @@ case class Run(
maybeMain.flatMap { main =>
val params = new RunParams(buildTarget(started.buildPaths, project))
val mainClass = new ScalaMainClass(main, args.asJava, List(s"-Duser.dir=${started.prebootstrapped.buildPaths.cwd}").asJava)
mainClass.setEnvironmentVariables(Nil.asJava)
params.setData(mainClass)
params.setDataKind("scala-main-class")
started.logger.debug(params.toString)

bloop.server.buildTargetRun(params).get().getStatusCode match {
case bsp4j.StatusCode.OK => Right(started.logger.info("Run succeeded"))
case other => Left(new BspCommandFailed("Run", List(project), other))
def failed(reason: BspCommandFailed.Reason) =
Left(new BspCommandFailed("Run", List(project), reason))

Try(bloop.server.buildTargetRun(params).get().getStatusCode) match {
case Success(bsp4j.StatusCode.OK) => Right(started.logger.info("Run succeeded"))
case Success(errorCode) => failed(BspCommandFailed.StatusCode(errorCode))
case Failure(exception) =>
def findResponseError(th: Throwable): Option[ResponseError] =
th match {
case x: java.util.concurrent.ExecutionException =>
findResponseError(x.getCause)
case x: org.eclipse.lsp4j.jsonrpc.ResponseErrorException =>
Option(x.getResponseError)
case _ => None
}

findResponseError(exception) match {
case Some(responseError) => failed(BspCommandFailed.FoundResponseError(responseError))
case None => failed(BspCommandFailed.FailedWithException(exception))
}
}
}
}
Expand Down
4 changes: 2 additions & 2 deletions bleep-core/src/main/scala/bleep/commands/Test.scala
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@ case class Test(started: Started, projects: List[model.CrossProjectName]) extend
case bsp4j.StatusCode.OK =>
started.logger.info("Tests succeeded")
Right(())
case other =>
Left(new BspCommandFailed("tests", projects, other))
case errorCode =>
Left(new BspCommandFailed("tests", projects, BspCommandFailed.StatusCode(errorCode)))
}
}
}

0 comments on commit c7083d7

Please sign in to comment.