Skip to content

Commit e6f0538

Browse files
raboofennru
authored andcommitted
Better errors (#88)
1 parent 103a58d commit e6f0538

File tree

3 files changed

+18
-13
lines changed

3 files changed

+18
-13
lines changed

src/main/scala/com/lightbend/paradox/apidoc/ApidocDirective.scala

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -17,14 +17,14 @@
1717
package com.lightbend.paradox.apidoc
1818

1919
import com.lightbend.paradox.markdown.InlineDirective
20+
import com.lightbend.paradox.markdown.Writer
2021
import org.pegdown.Printer
2122
import org.pegdown.ast.DirectiveNode.Source
2223
import org.pegdown.ast.{DirectiveNode, Visitor}
2324

24-
class ApidocDirective(allClassesAndObjects: IndexedSeq[String], properties: Map[String, String])
25-
extends InlineDirective("apidoc") {
25+
class ApidocDirective(allClassesAndObjects: IndexedSeq[String], ctx: Writer.Context) extends InlineDirective("apidoc") {
2626
final val JavadocProperty = raw"""javadoc\.(.*)\.base_url""".r
27-
final val JavadocBaseUrls = properties.collect {
27+
final val JavadocBaseUrls = ctx.properties.collect {
2828
case (JavadocProperty(pkg), url) => pkg -> url
2929
}
3030

@@ -86,7 +86,7 @@ class ApidocDirective(allClassesAndObjects: IndexedSeq[String], properties: Map[
8686
val regex = (query.pattern.replaceAll("\\.", "\\\\.").replaceAll("\\*", ".*") + "$").r
8787
allClasses.filter(cls => regex.findFirstMatchIn(cls).isDefined) match {
8888
case Seq() =>
89-
throw new java.lang.IllegalStateException(s"Class not found for @apidoc[$query]")
89+
ctx.error(s"Class not found for @apidoc[$query]", node)
9090
case results =>
9191
renderMatches(query, results, node, visitor, printer)
9292
}
@@ -139,9 +139,12 @@ class ApidocDirective(allClassesAndObjects: IndexedSeq[String], properties: Map[
139139

140140
matches.size match {
141141
case 0 =>
142-
throw new java.lang.IllegalStateException(s"No matches found for $query")
142+
ctx.error(s"No matches found for apidoc query [$query]", node)
143143
case 1 if matches(0).contains("adsl") =>
144-
throw new java.lang.IllegalStateException(s"Match for $query only found in one language: ${matches(0)}")
144+
ctx.error(
145+
s"Match for apidoc query [$query] only found in one language: ${matches(0)}",
146+
node
147+
)
145148
case 1 =>
146149
val pkg = matches(0)
147150
syntheticNode("scala", "scala", query.scalaLabel(pkg), pkg + scalaClassSuffix, sAnchor, node).accept(visitor)
@@ -163,9 +166,10 @@ class ApidocDirective(allClassesAndObjects: IndexedSeq[String], properties: Map[
163166
}
164167
})
165168
case n =>
166-
throw new java.lang.IllegalStateException(
169+
ctx.error(
167170
s"$n matches found for $query, but not javadsl/scaladsl: ${matches.mkString(", ")}. " +
168-
s"You may want to use the fully qualified class name as @apidoc[fqcn] instead of @apidoc[$query]."
171+
s"You may want to use the fully qualified class name as @apidoc[fqcn] instead of @apidoc[$query].",
172+
node
169173
)
170174
}
171175
}

src/main/scala/com/lightbend/paradox/apidoc/ApidocPlugin.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ object ApidocPlugin extends AutoPlugin {
5555
Def.task {
5656
Seq(
5757
{ ctx: Writer.Context =>
58-
new ApidocDirective(allClasses, ctx.properties)
58+
new ApidocDirective(allClasses, ctx)
5959
}
6060
)
6161
}

src/test/scala/com/lightbend/paradox/apidoc/ApidocDirectiveSpec.scala

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616

1717
package com.lightbend.paradox.apidoc
1818

19+
import com.lightbend.paradox.ParadoxException
1920
import com.lightbend.paradox.markdown.Writer
2021

2122
class ApidocDirectiveSpec extends MarkdownBaseSpec {
@@ -53,7 +54,7 @@ class ApidocDirectiveSpec extends MarkdownBaseSpec {
5354
verbatimSerializers = Writer.defaultVerbatims,
5455
serializerPlugins = Writer.defaultPlugins(
5556
Writer.defaultDirectives ++ Seq(
56-
(ctx: Writer.Context) => new ApidocDirective(allClasses, ctx.properties)
57+
(ctx: Writer.Context) => new ApidocDirective(allClasses, ctx)
5758
)
5859
)
5960
)
@@ -77,9 +78,9 @@ class ApidocDirectiveSpec extends MarkdownBaseSpec {
7778
}
7879

7980
it should "throw an exception when there is no match" in {
80-
val thrown = the[IllegalStateException] thrownBy markdown("@apidoc[ThereIsNoSuchClass]")
81+
val thrown = the[ParadoxException] thrownBy markdown("@apidoc[ThereIsNoSuchClass]")
8182
thrown.getMessage shouldEqual
82-
"No matches found for ThereIsNoSuchClass"
83+
"No matches found for apidoc query [ThereIsNoSuchClass]"
8384
}
8485

8586
it should "generate markdown correctly when 2 matches found and their package names include javadsl/scaladsl" in {
@@ -113,7 +114,7 @@ class ApidocDirectiveSpec extends MarkdownBaseSpec {
113114
}
114115

115116
it should "throw an exception when two matches found but javadsl/scaladsl is not in their packages" in {
116-
val thrown = the[IllegalStateException] thrownBy markdown("@apidoc[ActorRef]")
117+
val thrown = the[ParadoxException] thrownBy markdown("@apidoc[ActorRef]")
117118
thrown.getMessage shouldEqual
118119
"2 matches found for ActorRef, but not javadsl/scaladsl: akka.actor.ActorRef, akka.actor.typed.ActorRef. You may want to use the fully qualified class name as @apidoc[fqcn] instead of @apidoc[ActorRef]."
119120
}

0 commit comments

Comments
 (0)