Skip to content

Refactor: Improve error messaging in sidebar YAML parser #17229

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 7 commits into from
Jun 12, 2023

Conversation

Dedelweiss
Copy link
Contributor

@Dedelweiss Dedelweiss commented Apr 11, 2023

Validations

On this PR I added some errors for different conditions :

  • Error when a path in page is wrong

  • Error when an index or a page path for a title is missing
    (It is therefore activated with the title.isEmpty condition. So far I've only found that when a user forgets a title it gives r = RawInput(,,,[],,false) in the toSidebar function. Thus, title.isEmpty is necessarily empty. However, if another error produces an r = RawInput(,,[],,false). This condition and its error message will no longer make sense. I am therefore less sure about this condition.)

  • Error when a title for a page is missing

  • Warning when something is wrong but it does not interfere with the generation.

Tests

I added two functions in SidebarParserTest.
The purpose of these functions is to check if it contains the schemaMessage which is the general template sent in all error messages + the unique error message for each error.

IE

I also noticed that @gringrape wanted to work on this issue, if a code has already been done on his side, I am quite open to resume the work started or to close my PR to leave his. I am also open to any suggestions for adding errors.

Fixes: #15326

@julienrf
Copy link
Contributor

julienrf commented Apr 12, 2023

Hey @Dedelweiss, thank you very much for working on this!

About the first point: check the code that calls Sidebar.toSidebar, at some point you should get the root directory of the documentation files. Then you have two options: either pass the root directory to Sidebar.toSidebar and perform the check that the files exist here, or perform the check in the part of the code that calls Sidebar.toSidebar (that part of the code should have both the documentation root directory and the parsed sidebar). Does that make sense? Note that that could be achieved in a separate PR, if you want to keep things simple.

@Dedelweiss
Copy link
Contributor Author

About the first point: check the code that calls Sidebar.toSidebar, at some point you should get the root directory of the documentation files. Then you have two options: either pass the root directory to Sidebar.toSidebar and perform the check that the files exist here

Hello @julienrf ! Thank you very much, it works perfectly.

@Dedelweiss Dedelweiss marked this pull request as ready for review April 12, 2023 13:07
@Dedelweiss Dedelweiss changed the title Improve error messaging in sidebar YAML parser Feat: Improve error messaging in sidebar YAML parser Apr 12, 2023
@KacperFKorban KacperFKorban requested review from Florian3k and removed request for pikinier20 April 19, 2023 22:09
Copy link
Contributor

@Florian3k Florian3k left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good, I left one comment.

Copy link
Contributor

@julienrf julienrf left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there a possibility to create tests that cover this feature?

@Florian3k Florian3k self-requested a review May 5, 2023 07:44
Copy link
Contributor

@Florian3k Florian3k left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry, I just realized there are some problems with validation logic. I will get back to you with more precise information soon.

@Dedelweiss
Copy link
Contributor Author

Dedelweiss commented May 5, 2023

Yes, I'm currently working on it, sorry.

@Dedelweiss Dedelweiss force-pushed the error_sidebar branch 2 times, most recently from fd43076 to 4a668a8 Compare May 8, 2023 13:06
@Dedelweiss
Copy link
Contributor Author

Dedelweiss commented May 9, 2023

Edited: Just Added a new method to correct this. 👍

Hi, I'm working on the test and fixing the problem of the error message when the sidebar is correctly formatted.
I have sent a first commit for this. In this commit I changed the last one to a warning. Because even the static site is generated, there is a problem in the generation. For example, when you put :

- title: myFoo
   page: page1
   page: page2

Now I'm not sure if I missed any errors and I'm testing this.
For the test, I haven't finished the one where the title is missing. Because I don't get a single error message like :
report.error(s"$msg\n$schemaMessage")
But several errors and an exception like this:

Static site YAML configuration file should comply with the following description:
The root element of static site needs to be <subsection>
`title` and `directory` properties are ignored in root subsection.

<subsection>:
  title: <string> # optional - Default value is file name. Title can be also set using front-matter.
  index: <string> # optional - If not provided, default empty index template is generated.
  directory: <string> # optional - By default, directory name is title name in kebab case.
  subsection: # optional - If not provided, pages are loaded from the index directory
    - <subsection> | <page>
  # either index or subsection needs to be present
<page>:
  title: <string> # optional - Default value is file name. Title can be also set using front-matter.
  page: <string>
  hidden: <boolean> # optional - Default value is false.

For more information visit:
https://docs.scala-lang.org/scala3/guides/scaladoc/static-site.html: com.fasterxml.jackson.databind.exc.MismatchedInputException: Cannot deserialize value of type `java.util.ArrayList<dotty.tools.scaladoc.site.Sidebar$RawInput>` from Object value (token `JsonToken.START_OBJECT`)
 at [Source: (StringReader); line: 3, column: 5] (through reference chain: dotty.tools.scaladoc.site.Sidebar$RawInput["subsection"])
        at com.fasterxml.jackson.databind.exc.MismatchedInputException.from(MismatchedInputException.java:59)
        at com.fasterxml.jackson.databind.DeserializationContext.reportInputMismatch(DeserializationContext.java:1741)
        at com.fasterxml.jackson.databind.DeserializationContext.handleUnexpectedToken(DeserializationContext.java:1515)
Error parsing YAML configuration file: Title is not provided.
Static site YAML configuration file should comply with the following description:
The root element of static site needs to be <subsection>
`title` and `directory` properties are ignored in root subsection.

<subsection>:
  title: <string> # optional - Default value is file name. Title can be also set using front-matter.
  index: <string> # optional - If not provided, default empty index template is generated.
  directory: <string> # optional - By default, directory name is title name in kebab case.
  subsection: # optional - If not provided, pages are loaded from the index directory
    - <subsection> | <page>
  # either index or subsection needs to be present
<page>:
  title: <string> # optional - Default value is file name. Title can be also set using front-matter.
  page: <string>
  hidden: <boolean> # optional - Default value is false.

For more information visit:
https://docs.scala-lang.org/scala3/guides/scaladoc/static-site.html
Root element is not a subsection.
Static site YAML configuration file should comply with the following description:
The root element of static site needs to be <subsection>
`title` and `directory` properties are ignored in root subsection.

<subsection>:
  title: <string> # optional - Default value is file name. Title can be also set using front-matter.
  index: <string> # optional - If not provided, default empty index template is generated.
  directory: <string> # optional - By default, directory name is title name in kebab case.
  subsection: # optional - If not provided, pages are loaded from the index directory
    - <subsection> | <page>
  # either index or subsection needs to be present
<page>:
  title: <string> # optional - Default value is file name. Title can be also set using front-matter.
  page: <string>
  hidden: <boolean> # optional - Default value is false.

For more information visit:
https://docs.scala-lang.org/scala3/guides/scaladoc/static-site.html

I think because the title is missing, it goes to .fold and then to case _ in the toSidebar match. So I'm looking for a way to get a single error message.

@Dedelweiss Dedelweiss requested review from Florian3k and julienrf May 11, 2023 12:12
Copy link
Contributor

@julienrf julienrf left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you for adding tests @Dedelweiss! I have left a couple of comments.

case RawInput(title, page, index, subsection, dir, hidden) =>
report.error(s"Error parsing YAML configuration file.\n$schemaMessage")
if title.isEmpty() then
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why are we checking this? title is optional

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So it's optional for subsection, - page but not for a simple page from what I have seen in my tests. Example:

  - title: My title
    page: my-page1.md

But I noticed that it works with an index before (it's just ignored), so I'm thinking of adding && index.isEmpty.

- index: my-page2.md
   page: my-page1.md

Copy link
Contributor Author

@Dedelweiss Dedelweiss May 12, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

and this one works too:

index: index.md
subsection:
  - page: my-page1.md
    page: my-page2.md
  - page: my-page3.md

But the pages rendered are my-page2.md & my-page3.md

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
- Change the case match (Overkill) to a if else
- Two types of messages :
	- Error: NoTitle and No index or No page.
	- Warning: When the parsing is not done correctly. Example: When a title got two pages.
- Error when a title but no pages
- Do a new method of test (Use a 'contains' instead)
- Improve the accuracy of the error message by putting variables.
report.error(s"$msg\n$schemaMessage")
else
val msg = s"Error parsing YAML configuration file."
report.warning(s"$msg\n$schemaMessage")
Sidebar.Page(None, page, hidden)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why do we still return a Sidebar even though we reported errors?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Indeed, I think there's a problem with this return. Because we need to return the Sidebar type but with Sidebar.Page(None, page, hidden). When a user forgets a page associated with a title like :

index: index.md
subsection :
  - title : My title
  - page: my-page2.md

This sends an error but also blocks the generation of all the documentation (which is rather extreme).
See if I can return Sidebar.Category(None, None, Nil, None) instead.

@julienrf
Copy link
Contributor

Now, I don't know if it's better to let an else in case I missed a possible error and the user still gets an error message.
If you know of any other possible errors or have any feedback I am very interested.

How is it that you know the sidebar is not valid, but you don’t know what is invalid?

@Dedelweiss
Copy link
Contributor Author

How is it that you know the sidebar is not valid, but you don’t know what is invalid?

So, I think I've tested all the possible errors with a minimised code but I thought that the security of sending an error if a user didn't pass one of my conditions wouldn't be bad.

Copy link
Contributor

@julienrf julienrf left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you Lucas!

@Dedelweiss Dedelweiss changed the title Feat: Improve error messaging in sidebar YAML parser Refactor: Improve error messaging in sidebar YAML parser Jun 8, 2023
Copy link
Member

@ckipp01 ckipp01 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just a small nit.

case RawInput(title, page, index, subsection, dir, hidden) =>
report.error(s"Error parsing YAML configuration file.\n$schemaMessage")
if title.isEmpty() && index.isEmpty() then
val msg = "`title` property is missing for some page."
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since we have access to page up above here, can't we specify which page is actually missing the title instead of just saying "some page"?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

From what I understood this is not possible, see #17229 (comment)

Copy link
Member

@ckipp01 ckipp01 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just a small nit.

@julienrf julienrf merged commit daa9934 into scala:main Jun 12, 2023
@julienrf
Copy link
Contributor

It seems the tests added in this PR fail on Windows, see https://github.com/lampepfl/dotty/actions/runs/5240593619/jobs/9461621127. @Dedelweiss, would you have time to look at the failures?

@Dedelweiss
Copy link
Contributor Author

It seems the tests added in this PR fail on Windows, see https://github.com/lampepfl/dotty/actions/runs/5240593619/jobs/9461621127. @Dedelweiss, would you have time to look at the failures?

Hello Julien, sure, I will look at it.

ckipp01 pushed a commit that referenced this pull request Jun 13, 2023
The purpose of this Pull request is to correct my PR
#17229
Sorry for this failure.
I have tested the following command locally and my tests pass.
`sbt ";dist/pack ;scala3-bootstrapped/compile
;scala3-bootstrapped/test"`

[test_windows_full]

Fixes: #17963
ckipp01 pushed a commit that referenced this pull request Jun 21, 2023
The goal of this PR is to re-add the path validation of the page in a
sidebar.yaml and improve the Testcase in the `SidebarParserTest` by
erasing the first title, so it's easier to understand the problem in it.

This PR is related to PRs: #17229 & #17959 

[test_windows_full]

Co-authored-by: Lucas Leblanc <[email protected]>
@Kordyjan Kordyjan added this to the 3.4.0 milestone Aug 2, 2023
Kordyjan pushed a commit that referenced this pull request Nov 17, 2023
Kordyjan pushed a commit that referenced this pull request Nov 17, 2023
The purpose of this Pull request is to correct my PR
#17229
Sorry for this failure.
I have tested the following command locally and my tests pass.
`sbt ";dist/pack ;scala3-bootstrapped/compile
;scala3-bootstrapped/test"`

[test_windows_full]

Fixes: #17963
[Cherry-picked d83aa49]
Kordyjan added a commit that referenced this pull request Nov 21, 2023
…o LTS (#18982)

Backports #17229 to the LTS branch.

PR submitted by the release tooling.
[skip ci]
Kordyjan pushed a commit that referenced this pull request Nov 23, 2023
The goal of this PR is to re-add the path validation of the page in a
sidebar.yaml and improve the Testcase in the `SidebarParserTest` by
erasing the first title, so it's easier to understand the problem in it.

This PR is related to PRs: #17229 & #17959 

[test_windows_full]

Co-authored-by: Lucas Leblanc <[email protected]>
[Cherry-picked cf50fe3]
Kordyjan pushed a commit that referenced this pull request Nov 29, 2023
The goal of this PR is to re-add the path validation of the page in a
sidebar.yaml and improve the Testcase in the `SidebarParserTest` by
erasing the first title, so it's easier to understand the problem in it.

This PR is related to PRs: #17229 & #17959 

[test_windows_full]

Co-authored-by: Lucas Leblanc <[email protected]>
[Cherry-picked cf50fe3]
@Kordyjan Kordyjan modified the milestones: 3.4.0, 3.3.2 Dec 14, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Improve error messaging in sidebar YAML parser
6 participants