Skip to content

Commit 80bfcd7

Browse files
committed
Improve error messaging in sidebar YAML parser
In this commit: - Added a error message when the path of a page is wrong - Added an error message when an inddex or a page path in a subsection is missing - Added an error message when a title in a subsection is missing
1 parent bcaa1ca commit 80bfcd7

File tree

1 file changed

+18
-4
lines changed

1 file changed

+18
-4
lines changed

scaladoc/src/dotty/tools/scaladoc/site/SidebarParser.scala

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import com.fasterxml.jackson.core.`type`.TypeReference;
77
import scala.jdk.CollectionConverters._
88
import java.util.Optional
99
import scala.beans._
10+
import java.nio.file.{Files, Paths}
1011

1112
enum Sidebar:
1213
case Category(
@@ -30,13 +31,26 @@ object Sidebar:
3031

3132
private object RawInputTypeRef extends TypeReference[RawInput]
3233

33-
private def toSidebar(r: RawInput)(using CompilerContext): Sidebar = r match
34+
private def toSidebar(r: RawInput, content: String | java.io.File)(using CompilerContext): Sidebar = r match
3435
case RawInput(title, page, index, subsection, dir, hidden) if page.nonEmpty && index.isEmpty && subsection.isEmpty() =>
36+
val sidebarPath = content match
37+
case s: String => Paths.get(s)
38+
case f: java.io.File => f.toPath()
39+
val basePath = sidebarPath.getParent().resolve("_docs")
40+
val pagePath = basePath.resolve(page)
41+
if !Files.exists(pagePath) then
42+
report.error(s"Page $page does not exist.")
3543
Sidebar.Page(Option.when(title.nonEmpty)(title), page, hidden)
3644
case RawInput(title, page, index, subsection, dir, hidden) if page.isEmpty && (!subsection.isEmpty() || !index.isEmpty()) =>
37-
Sidebar.Category(Option.when(title.nonEmpty)(title), Option.when(index.nonEmpty)(index), subsection.asScala.map(toSidebar).toList, Option.when(dir.nonEmpty)(dir))
45+
Sidebar.Category(Option.when(title.nonEmpty)(title), Option.when(index.nonEmpty)(index), subsection.asScala.map(toSidebar(_, content)).toList, Option.when(dir.nonEmpty)(dir))
3846
case RawInput(title, page, index, subsection, dir, hidden) =>
39-
report.error(s"Error parsing YAML configuration file.\n$schemaMessage")
47+
val errors = (title.isEmpty(), page.isEmpty(), index.isEmpty(), subsection.isEmpty(), dir.isEmpty())
48+
errors match
49+
case (true, _, _, _, _) =>
50+
report.error(s"Title is not provided.\n$schemaMessage")
51+
case (false, true, true, _, _) =>
52+
report.error(s"Index or page path to at least one page is missing.\n$schemaMessage")
53+
case _ => report.error(s"Error parsing YAML configuration file.\n$schemaMessage")
4054
Sidebar.Page(None, page, hidden)
4155

4256
private def schemaMessage: String =
@@ -75,7 +89,7 @@ object Sidebar:
7589
},
7690
identity
7791
)
78-
toSidebar(root) match
92+
toSidebar(root, content) match
7993
case c: Sidebar.Category => c
8094
case _ =>
8195
report.error(s"Root element is not a subsection.\n$schemaMessage")

0 commit comments

Comments
 (0)