fix(cli): escape JSON error output - #387
Open
NgoQuocViet2001 wants to merge 1 commit into
Open
Conversation
There was a problem hiding this comment.
No issues found across 1 file
Shadow auto-approve: would auto-approve. Fixes invalid JSON output by escaping error messages, with a shared formatter and a regression test covering quotes/control chars. No new behavior, exposure, or operational tradeoff; human sign-off is unnecessary.
Re-trigger cubic
The --json exit in the main conversion path interpolated the error
straight into its object:
println!(r#"{{"error":"{}"}}"#, e);
A PdfError message carrying a quote or a newline -- "missing required
dictionary key \"Pages\"" is the common one -- produced JSON that no
consumer could parse. The items-json path next to it already escapes.
Route both --json error exits through a format_error_json helper that
mirrors the existing format_ocr_error_json, so the two paths escape
identically and a test can pin the emitted object rather than the
escaper alone.
NgoQuocViet2001
force-pushed
the
fix-pdf2md-error-json
branch
from
August 20, 2026 09:11
5beba43 to
d94cfaa
Compare
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.
What
The
--jsonexit inpdf2md's main conversion path interpolates the error straight into its object:A
PdfErrormessage carrying a quote or a newline produces JSON that no consumer can parse.missing required dictionary key "Pages"is the everyday case:How
Both
--jsonerror exits now go through a smallformat_error_json, placed beside and mirroring the existingformat_ocr_error_json:The items-json exit was already calling
json_escapeinline, so routing it through the helper is not a behaviour change there — it just means the two paths cannot drift, and the emitted object is testable rather than only the escaper.I did not give this one a
schema_version, unlike the OCR envelope: that would change the shape of an output that already ships. Happy to add it if you'd rather the two converge.Rebased and reshaped
This PR was opened before #434 and had gone stale — resetting onto current
mainresolved the conflict, and it turned out the original patch was aimed at a site you have since fixed. The items-json path escapes onmaintoday; the conversion path is the one still open. So this is now a much smaller change against that remaining site rather than a rebase of the original diff.Test plan
cargo test --bin pdf2md— 4 passed, 0 failed, including the existingocr_json_errors_use_the_same_versioned_envelope.convert_json_errors_are_escapedasserts the full emitted object for a message containing both a quote and a newline.cargo clippy --bin pdf2md— no warnings.rustfmt --check src/bin/pdf2md.rs— clean.Not run:
cargo fmt --all -- --checkand the fullcargo test.cargo fmtacross this workspace overflows its stack on my Windows machine — it does so on unmodifiedmaintoo, so it is my environment rather than this change — which is why I checked the one touched file withrustfmtdirectly. Flagging it rather than letting CI be the first to find out. An earlier revision of this description claimed both had been run; that was carried over from the original August 14 version of this PR and is not accurate for this commit.