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)
This commit is contained in:
Assaf Gordon authored and Nicolas Williams committed 2015-05-21 18:49:24 -05:00
1 parent a50e548cc5
commit 7b6a018dff
5 files changed
+44 -6

No files matched your search

+16
View File
@@ -313,3 +313,19 @@ jv jv_dump_string(jv x, int flags) {
jvp_dtoa_context_free(&C);
return s;
}
char *jv_dump_string_trunc(jv x, char *outbuf, size_t bufsize) {
x = jv_dump_string(x,0);
const char* p = jv_string_value(x);
const size_t len = strlen(p);
strncpy(outbuf, p, bufsize);
outbuf[bufsize - 1] = 0;
if (len > bufsize - 1 && bufsize >= 4) {
// Indicate truncation with '...'
outbuf[bufsize - 2]='.';
outbuf[bufsize - 3]='.';
outbuf[bufsize - 4]='.';
}
jv_free(x);
return outbuf;
}