Skip to content

Commit

Permalink
throw Result.Failure for prettier error messages
Browse files Browse the repository at this point in the history
  • Loading branch information
lolgab committed Aug 22, 2024
1 parent b4c9386 commit 0c0fd7c
Show file tree
Hide file tree
Showing 8 changed files with 25 additions and 23 deletions.
4 changes: 2 additions & 2 deletions main/server/src/mill/main/server/Server.scala
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import scala.jdk.CollectionConverters._
import org.newsclub.net.unix.AFUNIXServerSocket
import org.newsclub.net.unix.AFUNIXSocketAddress
import mill.main.client._
import mill.api.SystemStreams
import mill.api.{Result, SystemStreams}
import mill.main.client.ProxyStream.Output
import mill.main.client.lock.{Lock, Locks}

Expand Down Expand Up @@ -63,7 +63,7 @@ abstract class Server[T](
}
) ()

}.getOrElse(throw new Exception("Mill server process already present"))
}.getOrElse(throw Result.Failure("Mill server process already present"))
finally exitServer()
}

Expand Down
3 changes: 2 additions & 1 deletion main/util/src/mill/util/Jvm.scala
Original file line number Diff line number Diff line change
Expand Up @@ -185,7 +185,8 @@ object Jvm extends CoursierSupport {
Runtime.getRuntime().removeShutdownHook(shutdownHook)
}
if (process.exitCode() == 0) ()
else throw new Exception("Interactive Subprocess Failed (exit code " + process.exitCode() + ")")
else
throw Result.Failure("Interactive Subprocess Failed (exit code " + process.exitCode() + ")")
}

/**
Expand Down
8 changes: 4 additions & 4 deletions scalalib/api/src/mill/scalalib/api/ZincWorkerUtil.scala
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package mill.scalalib.api

import mill.api.Loose.Agg
import mill.api.PathRef
import mill.api.{PathRef, Result}
import scala.util.matching.Regex

trait ZincWorkerUtil {
Expand Down Expand Up @@ -38,7 +38,7 @@ trait ZincWorkerUtil {

classPath.iterator
.find(pathRef => mavenStyleMatch(pathRef.path.last) || ivyStyleMatch(pathRef.path))
.getOrElse(throw new Exception(
.getOrElse(throw Result.Failure(
s"Cannot find **/$name-$versionPrefix*$suffix or **/$versionPrefix*/$dir/$name$suffix in ${classPath.iterator.mkString("[", ", ", "]")}"
))
}
Expand Down Expand Up @@ -68,7 +68,7 @@ trait ZincWorkerUtil {

def scalaJSBinaryVersion(scalaJSVersion: String): String = scalaJSVersion match {
case _ if scalaJSVersion.startsWith("0.6.") =>
throw new Exception("Scala.js 0.6 is not supported")
throw Result.Failure("Scala.js 0.6 is not supported")
case ScalaJSFullVersion(major, minor, patch, suffix) =>
if (suffix != null && minor == "0" && patch == "0")
s"$major.$minor$suffix"
Expand All @@ -78,7 +78,7 @@ trait ZincWorkerUtil {

def scalaJSWorkerVersion(scalaJSVersion: String): String = scalaJSVersion match {
case _ if scalaJSVersion.startsWith("0.6.") =>
throw new Exception("Scala.js 0.6 is not supported")
throw Result.Failure("Scala.js 0.6 is not supported")
case ScalaJSFullVersion(major, _, _, _) =>
major
}
Expand Down
3 changes: 2 additions & 1 deletion scalalib/src/mill/scalalib/CrossModuleBase.scala
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package mill.scalalib

import mill.T
import mill.api.Result
import mill.define.Cross
import mill.define.Cross.Resolver
import mill.scalalib.api.ZincWorkerUtil
Expand Down Expand Up @@ -31,7 +32,7 @@ trait CrossModuleBase extends ScalaModule with Cross.Module[String] {
)
.collectFirst { case x => x }
.getOrElse(
throw new Exception(
throw Result.Failure(
s"Unable to find compatible cross version between $crossScalaVersion and " +
c.crossModules.map(_.crossScalaVersion).mkString(",")
)
Expand Down
12 changes: 6 additions & 6 deletions scalalib/src/mill/scalalib/Dep.scala
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
package mill.scalalib

import upickle.default.{macroRW, ReadWriter => RW}
import coursier.core.{Configuration, Dependency}
import mill.api.Result
import mill.scalalib.CrossVersion._
import coursier.core.Dependency
import mill.scalalib.api.ZincWorkerUtil
import upickle.default.{macroRW, ReadWriter => RW}

import scala.annotation.unused
import coursier.core.Configuration

case class Dep(dep: coursier.Dependency, cross: CrossVersion, force: Boolean) {
require(
Expand Down Expand Up @@ -110,8 +110,8 @@ object Dep {
s.split('=') match {
case Array("classifier", v) => as.withClassifier(coursier.Classifier(v))
case Array("type", v) => as.withType(coursier.Type(v))
case Array(k, v) => throw new Exception(s"Unrecognized attribute: [$s]")
case _ => throw new Exception(s"Unable to parse attribute specifier: [$s]")
case Array(k, v) => throw Result.Failure(s"Unrecognized attribute: [$s]")
case _ => throw Result.Failure(s"Unable to parse attribute specifier: [$s]")
}
}
(module.split(':') match {
Expand All @@ -121,7 +121,7 @@ object Dep {
case Array(a, "", b, "", c) => Dep(a, b, c, cross = Binary(platformed = true))
case Array(a, "", "", b, c) => Dep(a, b, c, cross = Full(platformed = false))
case Array(a, "", "", b, "", c) => Dep(a, b, c, cross = Full(platformed = true))
case _ => throw new Exception(s"Unable to parse signature: [$signature]")
case _ => throw Result.Failure(s"Unable to parse signature: [$signature]")
}).configure(attributes = attributes)
}
def apply(
Expand Down
2 changes: 1 addition & 1 deletion scalalib/src/mill/scalalib/PublishModule.scala
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ trait PublishModule extends JavaModule { outer =>
override def moduleDeps: Seq[PublishModule] = super.moduleDeps.map {
case m: PublishModule => m
case other =>
throw new Exception(
throw Result.Failure(
s"PublishModule moduleDeps need to be also PublishModules. $other is not a PublishModule"
)
}
Expand Down
2 changes: 1 addition & 1 deletion scalalib/src/mill/scalalib/ZincWorkerModule.scala
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ trait ZincWorkerModule extends mill.Module with OfflineSupportModule { self: Cou
scalaCompilerBridgeJar(x, y, repositoriesTask())
.asSuccess
.getOrElse(
throw new Exception(s"Failed to load compiler bridge for $x $y")
throw Result.Failure(s"Failed to load compiler bridge for $x $y")
)
.value
)),
Expand Down
14 changes: 7 additions & 7 deletions scalalib/src/mill/scalalib/publish/SonatypeHttpApi.scala
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,16 @@ import java.util.Base64
import scala.annotation.tailrec
import scala.concurrent.duration._

import mill.api.Result
import mill.main.BuildInfo
import requests.BaseSession
import ujson.ParseException
import requests.Session

class SonatypeHttpApi(
uri: String,
credentials: String,
readTimeout: Int,
connectTimeout: Int
) {
val http: Session = requests.Session(
val http: requests.Session = requests.Session(
readTimeout = readTimeout,
connectTimeout = connectTimeout,
maxRedirects = 0,
Expand All @@ -29,7 +27,9 @@ class SonatypeHttpApi(
"Authorization" -> s"Basic $base64Creds",
"Accept" -> "application/json",
"Content-Type" -> "application/json",
"User-Agent" -> s"mill-${BuildInfo.millVersion}${BaseSession.defaultHeaders.get("User-Agent").map(" (" + _ + ")").getOrElse("")}"
"User-Agent" -> s"mill-${BuildInfo.millVersion}${requests.BaseSession.defaultHeaders.get(
"User-Agent"
).map(" (" + _ + ")").getOrElse("")}"
)

// https://oss.sonatype.org/nexus-staging-plugin/default/docs/path__staging_profiles.html
Expand All @@ -42,7 +42,7 @@ class SonatypeHttpApi(
)

if (!response.is2xx) {
throw new Exception(s"$uri/staging/profiles returned ${response.statusCode}")
throw Result.Failure(s"$uri/staging/profiles returned ${response.statusCode}")
}

val resourceUri =
Expand All @@ -67,7 +67,7 @@ class SonatypeHttpApi(
try {
ujson.read(response.text())("type").str
} catch {
case e: ParseException =>
case e: ujson.ParseException =>
throw new RuntimeException(
s"Could not parse HTTP response. ${e.getMessage()}" + "\n" + s"Raw response: ${response}",
e
Expand Down

0 comments on commit 0c0fd7c

Please sign in to comment.