Skip to content

Commit

Permalink
Report filename:line on runtime errors (fix #752)
Browse files Browse the repository at this point in the history
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
  • Loading branch information
agordon authored and nicowilliams committed May 21, 2015
1 parent 2e25756 commit a6460f4
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 2 deletions.
1 change: 1 addition & 0 deletions jq.h
Original file line number Diff line number Diff line change
Expand Up @@ -46,5 +46,6 @@ void jq_util_input_add_input(jq_util_input_state, jv);
int jq_util_input_errors(jq_util_input_state);
jv jq_util_input_next_input(jq_util_input_state);
jv jq_util_input_next_input_cb(jq_state *, void *);
jv jq_util_input_get_position(jq_state*);

#endif /* !JQ_H */
8 changes: 6 additions & 2 deletions main.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
34 changes: 34 additions & 0 deletions util.c
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,8 @@ struct jq_util_input_state {
jv slurped;
char buf[4096];
size_t buf_valid_len;
jv current_filename;
size_t current_line;
};

static void fprinter(void *data, jv fname) {
Expand All @@ -178,6 +180,8 @@ jq_util_input_state jq_util_input_init(jq_msg_cb err_cb, void *err_cb_data) {
new_state->slurped = jv_invalid();
new_state->buf[0] = 0;
new_state->buf_valid_len = 0;
new_state->current_filename = jv_invalid();
new_state->current_line = 0;

return new_state;
}
Expand All @@ -204,6 +208,7 @@ void jq_util_input_free(jq_util_input_state *state) {
jv_parser_free(old_state->parser);
jv_free(old_state->files);
jv_free(old_state->slurped);
jv_free(old_state->current_filename);
jv_mem_free(old_state);
}

Expand Down Expand Up @@ -237,18 +242,24 @@ static int jq_util_input_read_more(jq_util_input_state state) {
fclose(state->current_input);
}
state->current_input = NULL;
jv_free(state->current_filename);
state->current_filename = jv_invalid();
state->current_line = 0 ;
}
jv f = next_file(state);
if (jv_is_valid(f)) {
if (!strcmp(jv_string_value(f), "-")) {
state->current_input = stdin;
state->current_filename = jv_string("<stdin>");
} else {
state->current_input = fopen(jv_string_value(f), "r");
state->current_filename = jv_copy(f);
if (!state->current_input) {
state->err_cb(state->err_cb_data, jv_copy(f));
state->failures++;
}
}
state->current_line = 0;
jv_free(f);
}
}
Expand All @@ -268,6 +279,9 @@ static int jq_util_input_read_more(jq_util_input_state state) {
state->failures++;
} else {
const char *p = memchr(state->buf, '\n', sizeof(state->buf));

if (p != NULL)
state->current_line++;

if (p == NULL && state->parser != NULL) {
/*
Expand Down Expand Up @@ -307,6 +321,26 @@ jv jq_util_input_next_input_cb(jq_state *jq, void *data) {
return jq_util_input_next_input((jq_util_input_state)data);
}

// Return the current_filename:current_line
jv jq_util_input_get_position(jq_state *jq) {
jq_input_cb cb = NULL;
void *cb_data = NULL;
jq_get_input_cb(jq, &cb, &cb_data);
assert(cb == jq_util_input_next_input_cb);
if (cb != jq_util_input_next_input_cb)
return jv_invalid_with_msg(jv_string("Invalid jq_util_input API usage"));
jq_util_input_state s = (jq_util_input_state)cb_data;

// We can't assert that current_filename is a string because if
// the error was a JSON parser error then we may not have set
// current_filename yet.
if (jv_get_kind(s->current_filename) != JV_KIND_STRING)
return jv_string("<unknown>");

jv v = jv_string_fmt("%s:%zu", jv_string_value(s->current_filename), s->current_line);
return v;
}

// Blocks to read one more input from stdin and/or given files
// When slurping, it returns just one value
jv jq_util_input_next_input(jq_util_input_state state) {
Expand Down

0 comments on commit a6460f4

Please sign in to comment.