Report malformed schema JSON as a compile error instead of panicking#1030
Open
danieleades wants to merge 1 commit into
Open
Report malformed schema JSON as a compile error instead of panicking#1030danieleades wants to merge 1 commit into
danieleades wants to merge 1 commit into
Conversation
Previously, import_types! unwrapped the serde_json parse result, so a syntax error in the schema file surfaced as an opaque "proc macro panicked" error. Map the parse error to a spanned syn::Error, matching the existing handling of file-open failures and preserving serde_json's line/column information.
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.
Problem
import_types!unwraps the result ofserde_json::from_reader, so a JSON syntax error in the schema file (stray comma, truncated file, ...) panics the proc macro. Users see an opaque error with no indication of what went wrong or where:Fix
Map the parse error to a spanned
syn::Error, matching the existing handling of file-open failures. serde_json's line/column information is preserved in the message:This addresses the malformed-JSON half of the diagnostics concerns raised in #516.
Trade-offs
None.
Testing
cargo test --workspace --lockedpasses.passcases (nocompile_failharness), so the before/after output above was verified manually with a scratch crate depending ontypifyby path with a malformedbad.json.