Skip to content

Commit

Permalink
Improve error reporting a bit further
Browse files Browse the repository at this point in the history
  • Loading branch information
MateuszKubuszok committed Sep 24, 2020
1 parent bc5a2ee commit c2a3d3e
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 17 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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"""#""")
Expand All @@ -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"))
}
}

0 comments on commit c2a3d3e

Please sign in to comment.