summaryrefslogtreecommitdiffstats
path: root/main.c
diff options
context:
space:
mode:
authorAssaf Gordon <assafgordon@gmail.com>2015-04-17 15:28:00 -0400
committerNicolas Williams <nico@cryptonector.com>2015-05-20 23:38:22 -0500
commita6460f4fe2ae9c1182a835e191684f1fabedffe8 (patch)
treed00af72e4b4525d3f6e70e55f590190bb20637b0 /main.c
parent2e257563a61dd0da1e4d76e2b79a7c8c80e9f0c3 (diff)
Report filename:line on runtime errors (fix #752)
With this patch, jq run-time errors printed to stderr will contain the filename and line of the offending input. But note that the JSON text parser does (yet) print the input filename on parse error. A jq run-time error is a jq program error, not including syntax errors nor JSON text input format errors. Examples: With stdin and multiple lines: $ printf '{"a":43}\n{"a":{"b":66}}\n' | ./jq '.a+1' 44 jq: error (at stdin:2): object and number cannot be added With multiple files: $ printf '{"a":43}' > 1.json $ printf '{"a":"hello"}\n' > 2.json $ printf '{"a":{"b":66}}\n' > 3.json $ ./jq '[.a]|@tsv' 1.json 2.json 3.json "43" "hello" jq: error (at 3.json:1): object is not valid in a csv row With very long lines (spanning multiple `fgets` calls): $ ( printf '{"a":43}\n' ; printf '{"a":{"b":[' ; seq 10000 | paste -d, -s | tr -d '\n' ; printf ']}}\n' ; printf '{"a":"hello"}\n' ) | ./jq '[.a] | @tsv' "43" jq: error (at stdin:2): object is not valid in a csv row "hello" With raw input: $ seq 1000 | ./jq --raw-input 'select(.=="700") | . + 10' jq: error (at stdin:700): string and number cannot be added Caveat: The reported line will be the last line of the (valid) parsed JSON data. Example: $ printf '{\n"a":\n"hello"\n\n\n}\n' | ./jq '.a+4' jq: error (at stdin:6): string and number cannot be added
Diffstat (limited to 'main.c')
-rw-r--r--main.c8
1 files changed, 6 insertions, 2 deletions
diff --git a/main.c b/main.c
index 31dbc824..2433e418 100644
--- a/main.c
+++ b/main.c
@@ -132,13 +132,17 @@ static int process(jq_state *jq, jv value, int flags, int dumpopts) {
if (jv_invalid_has_msg(jv_copy(result))) {
// Uncaught jq exception
jv msg = jv_invalid_get_msg(jv_copy(result));
+ jv input_pos = jq_util_input_get_position(jq);
if (jv_get_kind(msg) == JV_KIND_STRING) {
- fprintf(stderr, "jq: error: %s\n", jv_string_value(msg));
+ fprintf(stderr, "jq: error (at %s): %s\n",
+ jv_string_value(input_pos), jv_string_value(msg));
} else {
msg = jv_dump_string(msg, 0);
- fprintf(stderr, "jq: error (not a string): %s\n", jv_string_value(msg));
+ fprintf(stderr, "jq: error (at %s) (not a string): %s\n",
+ jv_string_value(input_pos), jv_string_value(msg));
}
ret = 5;
+ jv_free(input_pos);
jv_free(msg);
}
jv_free(result);