diff --git a/README.md b/README.md index 5be6757..34bf378 100644 --- a/README.md +++ b/README.md @@ -110,7 +110,7 @@ In order to do so it: You should be able to extend the abilities of the macro by expanding the content of `derive.semi.conf`. You can create this file and add it to your library if you want Catnip to support it as all files with that name are looked through during -compilation. When it comes to sbt it doesn't export resources to `Compile` scope, +compilation. When it comes to sbt it doesn't always export resources to `Compile` scope, so your configs might not be visible in your modules while they would be available in created JARs. (Creating somewhat inconsistent experience). Personally, I fixed this by adding something like @@ -123,7 +123,7 @@ val myProject = project.in(file("my-project")) to sbt. This will make your customizations immediately available to your modules. -Take a look at an [example](modules/catnip-custom-example) project to see how it works +Take a look at an [example](modules/catnip-custom-example) project to see how customization works in practice. ## Debugging diff --git a/modules/catnip/src/main/scala/io/scalaland/catnip/internals/DerivedImpl.scala b/modules/catnip/src/main/scala/io/scalaland/catnip/internals/DerivedImpl.scala index fa83f52..beb30b5 100644 --- a/modules/catnip/src/main/scala/io/scalaland/catnip/internals/DerivedImpl.scala +++ b/modules/catnip/src/main/scala/io/scalaland/catnip/internals/DerivedImpl.scala @@ -137,7 +137,8 @@ private[catnip] object DerivedImpl extends Loggers { val source = scala.io.Source.fromURL(url) try { Validated.valid( - source.getLines() + source + .getLines() .map(_.trim) .filterNot(_ startsWith raw"""//""") .filterNot(_ startsWith raw"""#""") @@ -162,24 +163,27 @@ private[catnip] object DerivedImpl extends Loggers { .sequence .map(_.fold(Map.empty[String, Config])(_ ++ _)) - private val mappingsE: ValidatedNel[String, Map[String, Config]] = loadConfig("derive.semi.conf") - private val stubsE: ValidatedNel[String, Map[String, Config]] = loadConfig("derive.stub.conf") + private val mappingsE: ValidatedNel[String, Map[String, Config]] = loadConfig("derive.semi.conf").map { map => + map.withDefault { key => + val msg = s"No semi definition found for a type class $key, available definitions:\n${map.mkString("\n")}" + throw new NoSuchElementException(msg) + } + } + private val stubsE: ValidatedNel[String, Map[String, Config]] = loadConfig("derive.stub.conf").map { map => + map.withDefault { key => + val msg = s"No stub definition found for object $key, available definitions:\n${map.mkString("\n")}" + throw new NoSuchElementException(msg) + } + } def impl(c: Context)(annottees: Seq[c.Expr[Any]]): c.Expr[Any] = (mappingsE, stubsE).tupled match { case Validated.Valid((mappings, stubs)) => - new DerivedImpl( - mappings.withDefault { key => - throw new NoSuchElementException( - s"No semi definition found for a type class $key, available:\n${mappings.mkString("\n")}" - ) - }, - stubs.withDefault { key => - throw new NoSuchElementException( - s"No stub definition found for object $key, available:\n${stubs.mkString("\n")}" - ) - } - )(c)(annottees).derive().asInstanceOf[c.Expr[Any]] + try { + new DerivedImpl(mappings, stubs)(c)(annottees).derive().asInstanceOf[c.Expr[Any]] + } catch { + case e: Throwable => c.abort(c.enclosingPosition, e.getMessage) + } case Validated.Invalid(errors) => c.abort(c.enclosingPosition, errors.mkString_("\n")) } }