From b098cde573dd8b3fd14b2ccb7fc8a3e45fc6a8d8 Mon Sep 17 00:00:00 2001 From: Albert Meltzer <7529386+kitbellew@users.noreply.github.com> Date: Tue, 26 Apr 2022 10:42:54 -0700 Subject: [PATCH] ScalafmtReflect: improve parseConfigWith error --- .../org/scalafmt/dynamic/ScalafmtReflect.scala | 18 ++++++++++++++++-- 1 file changed, 16 insertions(+), 2 deletions(-) diff --git a/scalafmt-dynamic/src/main/scala/org/scalafmt/dynamic/ScalafmtReflect.scala b/scalafmt-dynamic/src/main/scala/org/scalafmt/dynamic/ScalafmtReflect.scala index e1f3731f7e..346ebfb339 100644 --- a/scalafmt-dynamic/src/main/scala/org/scalafmt/dynamic/ScalafmtReflect.scala +++ b/scalafmt-dynamic/src/main/scala/org/scalafmt/dynamic/ScalafmtReflect.scala @@ -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._ @@ -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 =>