Skip to content

Commit

Permalink
Fix jqlang#419, improve Cannot index message (sortof)
Browse files Browse the repository at this point in the history
jv_get() doesn't know if it's being called in the context of a jq
program or not, so it can't produce a very useful error message when the
types of the to-be-indexed value and the key don't agree.  For now,
including the key (when it is a short string) in the error message is as
significant an improvement as is easy to make.
  • Loading branch information
nicowilliams committed Jun 17, 2014
1 parent e151a30 commit 7ee3b72
Showing 1 changed file with 14 additions and 3 deletions.
17 changes: 14 additions & 3 deletions jv_aux.c
Original file line number Diff line number Diff line change
Expand Up @@ -95,9 +95,20 @@ jv jv_get(jv t, jv k) {
jv_free(k);
v = jv_null();
} else {
v = jv_invalid_with_msg(jv_string_fmt("Cannot index %s with %s",
jv_kind_name(jv_get_kind(t)),
jv_kind_name(jv_get_kind(k))));
/*
* If k is a short string it's probably from a jq .foo expression or
* similar, in which case putting it in the invalid msg may help the
* user. The length 30 is arbitrary.
*/
if (jv_get_kind(k) == JV_KIND_STRING && jv_string_length_bytes(jv_copy(k)) < 30) {
v = jv_invalid_with_msg(jv_string_fmt("Cannot index %s with string \"%s\"",
jv_kind_name(jv_get_kind(t)),
jv_string_value(k)));
} else {
v = jv_invalid_with_msg(jv_string_fmt("Cannot index %s with %s",
jv_kind_name(jv_get_kind(t)),
jv_kind_name(jv_get_kind(k))));
}
jv_free(t);
jv_free(k);
}
Expand Down

0 comments on commit 7ee3b72

Please sign in to comment.