Skip to content

Commit

Permalink
ScalafmtReflect: improve parseConfigWith error
Browse files Browse the repository at this point in the history
  • Loading branch information
kitbellew committed Apr 27, 2022
1 parent e18b40b commit b098cde
Showing 1 changed file with 16 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package org.scalafmt.dynamic

import com.typesafe.config.ConfigFactory
import java.io.Closeable
import java.net.URLClassLoader
import java.nio.file.Path
import org.scalafmt.dynamic.exceptions._
import org.scalafmt.dynamic.utils.ReflectUtils._
Expand Down Expand Up @@ -58,10 +59,23 @@ case class ScalafmtReflect(

private def parseConfigWith(
f: => Try[Object]
)(g: Throwable => Throwable): Try[ScalafmtReflectConfig] =
)(g: Throwable => Throwable): Try[ScalafmtReflectConfig] = {
@inline def fail(e: Throwable) = Failure(g(e))
f.map { configured =>
new ScalafmtReflectConfig(this)(configured.invoke("get"))
}.recoverWith { case ReflectionException(e) => Failure(g(e)) }
}.recoverWith {
case e: NoSuchElementException => // Configured.NotOk.get
val msg = s"Config load error: ${e.getMessage}"
fail(new ScalafmtDynamicError(msg, e.getCause))
case e: ReflectiveOperationException =>
val msg = classLoader match {
case x: URLClassLoader => getCorruptedClassPath(version, x.getURLs)
case _ => "Config dynamic load error"
}
fail(new ScalafmtDynamicError(msg, e))
case e => fail(e)
}
}

def parseConfig(path: Path): Try[ScalafmtReflectConfig] =
parseConfigWith(parseConfigPost300(path)) { e =>
Expand Down

0 comments on commit b098cde

Please # to comment.