Print readable error messages in doc code blocks#2803
Open
hhugo wants to merge 2 commits into
Open
Conversation
Exercises six Lexer.Error variants triggered from inside {@ocaml[...]}
blocks. The expected .err files capture the current behaviour: the
catch-all branch at lib/Fmt_ast.ml stringifies the exception as
"invalid code block: Ocamlformat_parser_extended.Lexer.Error(_, _)",
which is unreadable. The next commit replaces these with human-readable
messages, and this test pins the change.
When parsing fails inside a `{@ocaml[...]}` block, the catch-all branch
formatted the exception with `Exn.pp`, which for `Lexer.Error` produced
the raw constructor pattern
`Ocamlformat_parser_extended.Lexer.Error(_, _)`. `Lexer.Error` (and most
other compiler-libs exceptions) register a printer via
`Location.register_error_of_exn`, but that table is separate from the
`Printexc` table `Exn.pp` consults, so the printer was never used.
Route the catch-all through `Location.error_of_exn` instead: if a
printer is registered, format its `main` message; otherwise fall back to
`Exn.pp`. This handles `Lexer.Error` and any other registered exception
(e.g. parser internals) without per-variant enumeration.
The .err diff against the previous commit shows the before/after for the
six variants the regression test exercises.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
{@ocaml[...]}block, the catch-all branch inFmt_ast.mlformatted the exception withExn.pp, which forLexer.Errorproduced the raw constructor patternOcamlformat_parser_extended.Lexer.Error(_, _). The printer registered viaLocation.register_error_of_exnlives in a separate table fromPrintexc, so it was never consulted.Location.error_of_exn: if a printer is registered, format itsmainmessage; otherwise fall back toExn.pp. HandlesLexer.Errorand any other registered exception without per-variant enumeration.doc_lexer_errors.mld) covering the variants exercised.Test plan
dune builddune runtest(new.errfixtures updated)