Skip to content

Print readable error messages in doc code blocks#2803

Open
hhugo wants to merge 2 commits into
mainfrom
fix-lexer-error-message
Open

Print readable error messages in doc code blocks#2803
hhugo wants to merge 2 commits into
mainfrom
fix-lexer-error-message

Conversation

@hhugo
Copy link
Copy Markdown
Collaborator

@hhugo hhugo commented May 26, 2026

Summary

  • When parsing fails inside a {@ocaml[...]} block, the catch-all branch in Fmt_ast.ml formatted the exception with Exn.pp, which for Lexer.Error produced the raw constructor pattern Ocamlformat_parser_extended.Lexer.Error(_, _). The printer registered via Location.register_error_of_exn lives in a separate table from Printexc, so it was never consulted.
  • Route the catch-all through Location.error_of_exn: if a printer is registered, format its main message; otherwise fall back to Exn.pp. Handles Lexer.Error and any other registered exception without per-variant enumeration.
  • Add a regression test (doc_lexer_errors.mld) covering the variants exercised.

Test plan

  • dune build
  • dune runtest (new .err fixtures updated)

hhugo added 2 commits May 26, 2026 10:39
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.
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.

1 participant