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

cgen, json: make errors more informative (resolve empty panics) #22898

Merged
merged 4 commits into from
Nov 18, 2024

Conversation

ttytm
Copy link
Member

@ttytm ttytm commented Nov 18, 2024

The changes make error messages for json.decode more informative. The reason is that error messages we'll get when walking back on the cJSON error_ptr can currently be empty and become hard to localize. The example below would currently just print an empty line.

json_txt := 'abc'
_ := json.decode(Foo2, json_txt) or {
	eprintln(err)
}

With the changes the errors message failed to decode JSON string is added. Similar to failed to open file during a os.read_file error.


This should help with the first of the two problems initially attempted to solve in #21184.
The second example in the linked PR could be addressed in a separate PR, where cgen is extended to add a check to verify the variable that should be decoded for validity.

Huly®: V_0.6-21341

@spytheman
Copy link
Member

I've tried this on master, and you are right - the error is empty :-| ...

import json

struct Foo2 {}

json_txt := 'abc'
x := json.decode(Foo2, json_txt) or { panic(err) }
dump(x)

Copy link
Member

@spytheman spytheman left a comment

Choose a reason for hiding this comment

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

Excellent work.

@spytheman spytheman merged commit 8c4de20 into vlang:master Nov 18, 2024
72 checks passed
@ttytm ttytm deleted the cgen/json-update-errors branch November 18, 2024 14:25
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