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

Make EOF in JSON.parse human-oriented. #510

Merged
merged 1 commit into from
May 31, 2023
Merged

Conversation

olegbespalov
Copy link
Contributor

What?

Wrap the io.EOF in JSON.parse to make it human-oriented.

Why?

Resolves: #503

builtin_json.go Outdated
@@ -20,6 +21,9 @@ func (r *Runtime) builtinJSON_parse(call FunctionCall) Value {
d := json.NewDecoder(strings.NewReader(call.Argument(0).toString().String()))

value, err := r.builtinJSON_decodeValue(d)
if errors.Is(err, io.EOF) {
err = fmt.Errorf("Unexpected end of JSON input (%w)", err)
Copy link
Owner

Choose a reason for hiding this comment

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

It doesn't really make sense to create an error, considering we're only interested in its string value. Also, Go convention is not to begin an error message with a capital letter, so some IDEs would highlight this. It's better to do something like:

panic(r.newError(r.global.SyntaxError, fmt.Sprintf("Unexpected end of JSON input (%v)", err)))

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Sure, that makes sense. I've adjusted 👍

@dop251
Copy link
Owner

dop251 commented May 31, 2023

Thanks!

@imiric imiric mentioned this pull request Jun 1, 2023
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.

Improve JSON.parse error message
2 participants