Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve JSON parse error message #318

Merged
merged 1 commit into from
Nov 30, 2022

Conversation

timostamm
Copy link
Member

We already provide helpful error messages when JSON input does not have the expected shape. But Message.fromJsonString() does not. This wraps errors raised by JSON.parse(), and adds a prefix with the message type name.

// currently:
Example.fromJsonString("this is not json") // Unexpected token h in JSON at position 1

// with this PR:
Example.fromJsonString("this is not json") // cannot decode Example from JSON: Unexpected token h in JSON at position 1

We already provide helpful error messages when JSON input does not have the expected shape. But Message.fromJsonString() passes any JSON syntax errors unmodified. This wraps those errors and adds a prefix with the message type name.
@timostamm timostamm requested a review from smaye81 November 30, 2022 16:27
@@ -71,6 +71,14 @@ describe("google.protobuf.Value", () => {
});
expect(value.toJsonString()).toBe("true");
});
test("encoding unset value to JSON raises error", () => {
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Unrelated - just adding some coverage here because it is an odd case.

Copy link
Member

@smaye81 smaye81 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice improvement.

@timostamm timostamm merged commit 35c96ea into main Nov 30, 2022
@timostamm timostamm deleted the tstamm/slightly-improve-json-decoding-error-message branch November 30, 2022 19:25
@smaye81 smaye81 mentioned this pull request Dec 5, 2022
smaye81 added a commit that referenced this pull request Dec 5, 2022
This release includes the following:

## Enhancements
* Add plugin option `import_extension` to replace the `.js` extension in
import paths by @timostamm in #317
*  Improve JSON parse error message by @timostamm in #318
*  Make PlainMessage recursive by @smaye81 in #308

## New Contributors
@McTano made their first contribution in #311
smaye81 pushed a commit that referenced this pull request Dec 8, 2022
smaye81 added a commit that referenced this pull request Dec 8, 2022
This release includes the following:

## Enhancements
* Add plugin option `import_extension` to replace the `.js` extension in
import paths by @timostamm in #317
*  Improve JSON parse error message by @timostamm in #318
*  Make PlainMessage recursive by @smaye81 in #308

## New Contributors
@McTano made their first contribution in #311
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.

2 participants