Skip to content

Commit

Permalink
Include filename in input parse error message
Browse files Browse the repository at this point in the history
Fixes #1553
  • Loading branch information
wader committed Jun 30, 2024
1 parent ba741e5 commit 94abc60
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 10 deletions.
7 changes: 5 additions & 2 deletions src/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -731,14 +731,17 @@ int main(int argc, char* argv[]) {

// Parse error
jv msg = jv_invalid_get_msg(value);
jv input_pos = jq_util_input_get_position(jq);
if (!(options & SEQ)) {
ret = JQ_ERROR_UNKNOWN;
fprintf(stderr, "jq: parse error: %s\n", jv_string_value(msg));
fprintf(stderr, "jq: parse error (at %s): %s\n", jv_string_value(input_pos), jv_string_value(msg));
jv_free(input_pos);
jv_free(msg);
break;
}
// --seq -> errors are not fatal
fprintf(stderr, "jq: ignoring parse error: %s\n", jv_string_value(msg));
fprintf(stderr, "jq: ignoring parse error (at %s): %s\n", jv_string_value(input_pos), jv_string_value(msg));
jv_free(input_pos);
jv_free(msg);
}
}
Expand Down
32 changes: 24 additions & 8 deletions tests/shtest
Original file line number Diff line number Diff line change
Expand Up @@ -103,28 +103,44 @@ if [ $n -ne $nref ]; then
exit 1
fi

## Test parse error message
echo 123 > $d/input_valid
echo 456 > $d/input_error_line2
echo asd >> $d/input_error_line2

cat > $d/expected_out <<EOF
123
456
EOF
cat > $d/expected_err <<EOF
jq: parse error (at $d/input_error_line2:2): Invalid numeric literal at line 4, column 0
EOF
$VALGRIND $Q $JQ -c . $d/input_valid $d/input_error_line2 > $d/out 2> $d/err || true
cmp $d/out $d/expected_out
cmp $d/err $d/expected_err

## Test JSON sequence support

cat > $d/expected <<EOF
jq: ignoring parse error: Truncated value at line 2, column 5
jq: ignoring parse error: Truncated value at line 2, column 25
jq: ignoring parse error: Truncated value at line 2, column 41
jq: ignoring parse error (at <stdin>:2): Truncated value at line 2, column 5
jq: ignoring parse error (at <stdin>:2): Truncated value at line 2, column 25
jq: ignoring parse error (at <stdin>:2): Truncated value at line 2, column 41
EOF
printf '1\0362 3\n[0,1\036[4,5]true"ab"{"c":4\036{}{"d":5,"e":6"\036false\n'|$VALGRIND $Q $JQ -ces --seq '. == [2,3,[4,5],true,"ab",{},false]' > /dev/null 2> $d/out
cmp $d/out $d/expected

cat > $d/expected <<EOF
jq: ignoring parse error: Truncated value at line 2, column 5
jq: ignoring parse error: Truncated value at line 2, column 25
jq: ignoring parse error: Truncated value at line 3, column 1
jq: ignoring parse error (at <stdin>:2): Truncated value at line 2, column 5
jq: ignoring parse error (at <stdin>:2): Truncated value at line 2, column 25
jq: ignoring parse error (at <stdin>:2): Truncated value at line 3, column 1
EOF
printf '1\0362 3\n[0,1\036[4,5]true"ab"{"c":4\036{}{"d":5,"e":6"false\n\036null'|$VALGRIND $Q $JQ -ces --seq '. == [2,3,[4,5],true,"ab",{},null]' > /dev/null 2> $d/out
cmp $d/out $d/expected

# Note that here jq sees no inputs at all but it still succeeds because
# --seq ignores parse errors
cat > $d/expected <<EOF
jq: ignoring parse error: Unfinished abandoned text at EOF at line 1, column 4
jq: ignoring parse error (at <stdin>:0): Unfinished abandoned text at EOF at line 1, column 4
EOF
printf '"foo' | $JQ -c --seq . > $d/out 2>&1
cmp $d/out $d/expected
Expand All @@ -137,7 +153,7 @@ cmp $d/out $d/expected

# Numeric values truncated by EOF are ignored
cat > $d/expected <<EOF
jq: ignoring parse error: Unfinished abandoned text at EOF at line 1, column 1
jq: ignoring parse error (at <stdin>:0): Unfinished abandoned text at EOF at line 1, column 1
EOF
printf '1' | $JQ -c --seq . > $d/out 2>&1
cmp $d/out $d/expected
Expand Down

0 comments on commit 94abc60

Please sign in to comment.