Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Include filename in input parse error message #3146

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

line 4 here is a bit confusing, i guess that is counting lines from all input files seen so far

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
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Here it's at <stdin>:2 and line 3 hmm, haven dugg much but i guess it's start of new value vs where it ended somehow?

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
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should probably be <stdin>:1

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
Loading