Backtracing for exceptions thrown while parsing page frontmatter and content
- Dominant language
- PHP
- Stars
- 15.7k
- Forks
- 1.4k
- Avg merge
- 1d 3h
- Merged PRs (30d)
- 12
Description
When an exception is thrown due to malformed or otherwise bad page content, the Whoops debugging page traces the error through the code that _generated_ the exception, but does not trace to the actual line and content in the markdown file itself.
It is often possible to locate the source of the error by looking at the specific arguments in the "Arguments" section of the Whoops debug page and then doing a "find in files" (or the equivalent) in your text editor/IDE. However, it would be more efficient if the error page could directly trace the problem all the way to a line in the Markdown file itself.
Twig can already do this through `Twig_Error`. Grav pages are a little more complicated though, because they can contain frontmatter, Markdown, and/or Twig.
Frontmatter is parsed by the Symfony/Yaml library, and gets [read in as a single array](https://github.com/getgrav/grav/blob/develop/system/src/Grav/Common/Page/Page.php#L333-L334). This means that only Yaml parse errors will get caught in the context of the source page file. By the time any other errors are generated due to problems in the page content (for example, an unparseable date by the `DateTime` class), the original `.md` file has been discarded and Grav is left working only with a PHP array. Unless Symfony/Yaml has a mechanism for preserving the associations between the parsed contents and the original source file, I'm not sure how this could be done. At the very least the trace could identify the source file, if not the specific line in that file.
The process is similar for Markdown content, which is read into a single array and then passed to the `erusev/parsedown` library to be rendered. I'm not sure if there is even such thing as unparseable Markdown, but some of the Grav goodies for media, custom tags, etc could conceivably raise an exception. In this case again, there would ideally be a way to preserve an association between individual lines in the source `.md` file, and the array of parsed data.
Contributor guide
Assessment
This issue has not been assessed yet.