From 7ee3b724d14878d3820172419b9d07d5b7bd8948 Mon Sep 17 00:00:00 2001 From: Nicolas Williams Date: Tue, 17 Jun 2014 17:00:23 -0500 Subject: [PATCH] Fix #419, improve Cannot index message (sortof) 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. --- jv_aux.c | 17 ++++++++++++++--- 1 file changed, 14 insertions(+), 3 deletions(-) diff --git a/jv_aux.c b/jv_aux.c index 3c5750bd..5f233e05 100644 --- a/jv_aux.c +++ b/jv_aux.c @@ -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); }