Skip to content

Commit

Permalink
Introduce a MillException to transport the error condition
Browse files Browse the repository at this point in the history
  • Loading branch information
lefou committed Sep 27, 2023
1 parent 3ddb64f commit 807814b
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 14 deletions.
7 changes: 7 additions & 0 deletions main/api/src/mill/api/MillException.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package mill.api

/**
* This exception is specifically handled in [[mill.runner.MillMain]] and [[mill.runner.MillServerMain]]. You can use it, if you need to exit Mill with a nice error message.
* @param msg The error message, to be displayed to the user.
*/
class MillException(msg: String) extends Exception(msg)
27 changes: 24 additions & 3 deletions runner/src/mill/runner/MillMain.scala
Original file line number Diff line number Diff line change
@@ -1,18 +1,38 @@
package mill.runner
import mill.main.BuildInfo

import java.io.{FileOutputStream, PrintStream}
import java.util.Locale
import scala.jdk.CollectionConverters._
import scala.util.Properties
import mill.java9rtexport.Export
import mill.api.{DummyInputStream, internal}
import mill.api.SystemStreams
import mill.api.{DummyInputStream, MillException, internal, SystemStreams}
import mill.bsp.{BspContext, BspServerResult}
import mill.main.BuildInfo
import mill.util.PrintLogger

import java.lang.reflect.InvocationTargetException
import scala.util.control.NonFatal

@internal
object MillMain {

def handleMillException[T](
err: PrintStream,
onError: => T
): PartialFunction[Throwable, (Boolean, T)] = {
case e: MillException =>
err.println(e.getMessage())
(false, onError)
case e: InvocationTargetException
if e.getCause != null && e.getCause.isInstanceOf[MillException] =>
err.println(e.getCause.getMessage())
(false, onError)
case NonFatal(e) =>
err.println("An unexpected error occurred")
throw e
(false, onError)
}

def main(args: Array[String]): Unit = {
val initialSystemStreams = new SystemStreams(System.out, System.err, System.in)
// setup streams
Expand Down Expand Up @@ -57,6 +77,7 @@ object MillMain {
userSpecifiedProperties0 = Map(),
initialSystemProperties = sys.props.toMap
)
catch handleMillException(runnerStreams.err, ())
finally {
cleanupStreams.foreach(_.close())
}
Expand Down
23 changes: 12 additions & 11 deletions runner/src/mill/runner/MillServerMain.scala
Original file line number Diff line number Diff line change
Expand Up @@ -71,17 +71,18 @@ object MillServerMain extends MillServerMain[RunnerState] {
userSpecifiedProperties: Map[String, String],
initialSystemProperties: Map[String, String]
): (Boolean, RunnerState) = {
MillMain.main0(
args = args,
stateCache = stateCache,
mainInteractive = mainInteractive,
streams0 = streams,
bspLog = None,
env = env,
setIdle = setIdle,
userSpecifiedProperties0 = userSpecifiedProperties,
initialSystemProperties = initialSystemProperties
)
try MillMain.main0(
args = args,
stateCache = stateCache,
mainInteractive = mainInteractive,
streams0 = streams,
bspLog = None,
env = env,
setIdle = setIdle,
userSpecifiedProperties0 = userSpecifiedProperties,
initialSystemProperties = initialSystemProperties
)
catch MillMain.handleMillException(streams.err, stateCache)
}
}

Expand Down

0 comments on commit 807814b

Please sign in to comment.