fix(graphiql-react): plain-language message with line and column for invalid JSON - #4557
Open
vishwakt wants to merge 1 commit into
Open
fix(graphiql-react): plain-language message with line and column for invalid JSON#4557vishwakt wants to merge 1 commit into
vishwakt wants to merge 1 commit into
Conversation
…invalid JSON When the Variables or Headers pane contains invalid JSON, the response pane showed the bare jsonc-parser error code (e.g. "Variables are invalid JSON: ValueExpected.") in the same shape and place as a server response. Map jsonc-parser error codes to plain-language descriptions and include the line and column derived from the reported offset, e.g. "expected a value at line 1, column 8". Prefix the message with "Request not sent." so it is not mistaken for a server response. Add unit tests for the JSONC parsing utility. Refs graphql#4501.
🦋 Changeset detectedLatest commit: 1429549 The changes in this PR will be included in the next version bump. This PR includes changesets to release 1 package
Not sure what this means? Click here to learn what changesets are. Click here if you're a maintainer who wants to add another changeset to this PR |
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
When the Variables or Headers pane contains invalid JSON,
run()parses it client-side and writes an error into the response pane without sending a request. That error showed the barejsonc-parsererror code, in the same shape and place as a real server response:{ "errors": [{ "message": "Variables are invalid JSON: ValueExpected." }] }This makes the message readable and clearly client-side:
{ "errors": [{ "message": "Request not sent. Variables are invalid JSON: expected a value at line 1, column 8." }] }Refs #4501. This addresses the unfriendly message and the "looks like a server response" confusion from that issue. The request for a customization hook (a prop, callback, or plugin hook) is a public API decision, so it is intentionally left for maintainers to discuss on the issue.
Changes
utility/jsonc.ts: mapjsonc-parsererror codes to plain-language descriptions and include the line and column derived from the reportedoffset. Multiple errors are still listed together.stores/execution.ts: prefix the client-side validation error withRequest not sent.utility/jsonc.spec.tscovering the message format, line/column on multi-line input, multiple errors, and that valid JSONC (comments, trailing commas) still parses.How to verify
In the netlify preview (or a local GraphiQL):
query Q($id: Int!) { node(id: $id) { id } }{"id": }Before: the response pane shows
Variables are invalid JSON: ValueExpected.and it is not obvious that no request was made.After: it shows
Request not sent. Variables are invalid JSON: expected a value at line 1, column 8.and no request is sent.The same applies to the Headers pane, and to multi-line input, where the line and column point at the offending token.
Credit
Thanks to @limarkxx for the clear report, including pinpointing the exact code path.