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
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions vlib/json/tests/json_decode_test.v
Original file line number Diff line number Diff line change
Expand Up @@ -90,15 +90,15 @@ struct DbConfig {

fn test_decode_error_message_should_have_enough_context_empty() {
json.decode(DbConfig, '') or {
assert err.msg().len < 2
assert err.msg() == 'failed to decode JSON string'
return
}
assert false
}

fn test_decode_error_message_should_have_enough_context_just_brace() {
json.decode(DbConfig, '{') or {
assert err.msg() == '{'
assert err.msg() == 'failed to decode JSON string: {'
return
}
assert false
Expand All @@ -111,7 +111,7 @@ fn test_decode_error_message_should_have_enough_context_trailing_comma_at_end()
"user": "alex",
}'
json.decode(DbConfig, txt) or {
assert err.msg() == ' "user": "alex",\n}'
assert err.msg().contains(' "user": "alex",\n}')
return
}
assert false
Expand All @@ -120,7 +120,7 @@ fn test_decode_error_message_should_have_enough_context_trailing_comma_at_end()
fn test_decode_error_message_should_have_enough_context_in_the_middle() {
txt := '{"host": "localhost", "dbname": "alex" "user": "alex", "port": "1234"}'
json.decode(DbConfig, txt) or {
assert err.msg() == 'ost", "dbname": "alex" "user":'
assert err.msg().contains('ost", "dbname": "alex" "user":')
return
}
assert false
Expand Down
11 changes: 8 additions & 3 deletions vlib/v/gen/c/json.v
Original file line number Diff line number Diff line change
Expand Up @@ -95,11 +95,11 @@ ${dec_fn_dec} {
${init_styp};
if (!root) {
const char *error_ptr = cJSON_GetErrorPtr();
if (error_ptr != NULL) {
if (error_ptr != NULL) {
const int error_pos = (int)cJSON_GetErrorPos();
int maxcontext_chars = 30;
byte *buf = vcalloc_noscan(maxcontext_chars + 10);
if(error_pos > 0) {
if (error_pos > 0) {
int backlines = 1;
int backchars = error_pos < maxcontext_chars-7 ? (int)error_pos : maxcontext_chars-7 ;
char *prevline_ptr = (char*)error_ptr;
Expand All @@ -119,7 +119,12 @@ ${dec_fn_dec} {
int maxchars = vstrlen_char(prevline_ptr);
vmemcpy(buf, prevline_ptr, (maxchars < maxcontext_chars ? maxchars : maxcontext_chars));
}
return (${result_name}_${ret_styp}){.is_error = true,.err = _v_error(tos2(buf)),.data = {0}};
string msg;
msg = _SLIT("failed to decode JSON string");
if (buf[0] != \'\\0\') {
msg = string__plus(msg, _SLIT(": "));
}
return (${result_name}_${ret_styp}){.is_error = true,.err = _v_error(string__plus(msg, tos2(buf))),.data = {0}};
}
}
')
Expand Down
Loading