diff options
author | Assaf Gordon <assafgordon@gmail.com> | 2015-04-17 18:18:33 -0400 |
---|---|---|
committer | Nicolas Williams <nico@cryptonector.com> | 2015-05-21 18:49:24 -0500 |
commit | 7b6a018dff623a4f13f6bcd52c7c56d9b4a4165f (patch) | |
tree | 9e5a63bde790eb18aed024fc6ae39b0ec05056cf /builtin.c | |
parent | a50e548cc5313c187483bc8fb1b95e1798e8ef65 (diff) |
Print offending object in runtime error messages
When reporting an error to the user, add information about the offending
object/value (possibly truncated).
The goal is to give a user some context regarding which input object
caused the runtime error.
Examples:
$ echo '"hello"' | ./jq '-.'
jq: error: string ("hello") cannot be negated
$ echo '"very-long-string"' | ./jq '-.'
jq: error: string ("very-long-...) cannot be negated
$ echo '["1",2]' | ./jq '.|join(",")'
jq: error: string (",") and number (2) cannot be added
$ echo '["1","2",{"a":{"b":{"c":33}}}]' | ./jq '.|join(",")'
jq: error: string (",") and object ({"a":{"b":{...) cannot be added
$ echo '{"a":{"b":{"c":33}}}' | ./jq '.a | @tsv'
jq: error: object ({"b":{"c":33}}) cannot be tsv-formatted, only array
(Fix #754)
Diffstat (limited to 'builtin.c')
-rw-r--r-- | builtin.c | 9 |
1 files changed, 7 insertions, 2 deletions
@@ -22,17 +22,22 @@ static jv type_error(jv bad, const char* msg) { - jv err = jv_invalid_with_msg(jv_string_fmt("%s %s", + char errbuf[15]; + jv err = jv_invalid_with_msg(jv_string_fmt("%s (%s) %s", jv_kind_name(jv_get_kind(bad)), + jv_dump_string_trunc(jv_copy(bad), errbuf, sizeof(errbuf)), msg)); jv_free(bad); return err; } static jv type_error2(jv bad1, jv bad2, const char* msg) { - jv err = jv_invalid_with_msg(jv_string_fmt("%s and %s %s", + char errbuf1[15],errbuf2[15]; + jv err = jv_invalid_with_msg(jv_string_fmt("%s (%s) and %s (%s) %s", jv_kind_name(jv_get_kind(bad1)), + jv_dump_string_trunc(jv_copy(bad1), errbuf1, sizeof(errbuf1)), jv_kind_name(jv_get_kind(bad2)), + jv_dump_string_trunc(jv_copy(bad2), errbuf2, sizeof(errbuf2)), msg)); jv_free(bad1); jv_free(bad2); |