Skip to content

Commit 9289d16

Browse files
authored
Print nicer messages when encountering JSON parsing errors (#88)
Signed-off-by: Juan Cruz Viotti <jv@jviotti.com>
1 parent 9bd84cd commit 9289d16

File tree

9 files changed

+94
-6
lines changed

9 files changed

+94
-6
lines changed

DEPENDENCIES

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
vendorpull https://github.com/sourcemeta/vendorpull dea311b5bfb53b6926a4140267959ae334d3ecf4
22
noa https://github.com/sourcemeta/noa 2bc3138b80e575786bec418c91fc2058c6836993
3-
jsontoolkit https://github.com/sourcemeta/jsontoolkit 37580da97c6549cd5995b3ef62ad47892246ebfd
3+
jsontoolkit https://github.com/sourcemeta/jsontoolkit 1d291fc26bae59e778067e5dfa54096a1fb608f3
44
hydra https://github.com/sourcemeta/hydra 3c53d3fdef79e9ba603d48470a508cc45472a0dc

src/main.cc

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,14 @@ auto main(int argc, char *argv[]) noexcept -> int {
129129
} catch (const sourcemeta::jsontoolkit::SchemaResolutionError &error) {
130130
std::cerr << error.what() << ": " << error.id() << "\n";
131131
return EXIT_FAILURE;
132+
} catch (const sourcemeta::jsontoolkit::FileParseError &error) {
133+
std::cerr << error.path().string() << "\n " << error.what() << " at line "
134+
<< error.line() << " and column " << error.column() << "\n";
135+
return EXIT_FAILURE;
136+
} catch (const sourcemeta::jsontoolkit::ParseError &error) {
137+
std::cerr << error.what() << " at line " << error.line() << " and column "
138+
<< error.column() << "\n";
139+
return EXIT_FAILURE;
132140
} catch (const std::exception &error) {
133141
std::cerr << "Error: " << error.what() << "\n";
134142
return EXIT_FAILURE;

src/utils.cc

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@ auto handle_json_entry(
5353
return prefix == canonical ||
5454
path_starts_with(canonical, prefix);
5555
})) {
56+
// TODO: Print a verbose message for what is getting parsed
5657
result.emplace_back(canonical,
5758
sourcemeta::jsontoolkit::from_file(canonical));
5859
}
@@ -74,6 +75,7 @@ auto handle_json_entry(
7475
return prefix == canonical ||
7576
path_starts_with(canonical, prefix);
7677
})) {
78+
// TODO: Print a verbose message for what is getting parsed
7779
result.emplace_back(canonical,
7880
sourcemeta::jsontoolkit::from_file(canonical));
7981
}

test/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ add_jsonschema_test_unix(format_check_single_fail)
2424
add_jsonschema_test_unix(format_check_single_pass)
2525
add_jsonschema_test_unix(format_directory_ignore_directory)
2626
add_jsonschema_test_unix(format_directory_ignore_file)
27+
add_jsonschema_test_unix(format_check_single_invalid)
2728
add_jsonschema_test_unix(frame)
2829
add_jsonschema_test_unix(validate_pass_draft4)
2930
add_jsonschema_test_unix(validate_fail_draft4)
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
#!/bin/sh
2+
3+
set -o errexit
4+
set -o nounset
5+
6+
TMP="$(mktemp -d)"
7+
clean() { rm -rf "$TMP"; }
8+
trap clean EXIT
9+
10+
cat << 'EOF' > "$TMP/schema.json"
11+
{
12+
"$schema": "http://json-schema.org/draft-04/schema#"
13+
"type" 1,
14+
}
15+
EOF
16+
17+
"$1" fmt "$TMP/schema.json" --check 2>"$TMP/output.txt" && CODE="$?" || CODE="$?"
18+
19+
if [ "$CODE" = "0" ]
20+
then
21+
echo "FAIL" 1>&2
22+
exit 1
23+
fi
24+
25+
cat << EOF > "$TMP/expected.txt"
26+
$(realpath "$TMP/schema.json")
27+
Failed to parse the JSON document at line 3 and column 3
28+
EOF
29+
30+
diff "$TMP/output.txt" "$TMP/expected.txt"
31+
echo "PASS" 1>&2

vendor/jsontoolkit/src/json/include/sourcemeta/jsontoolkit/json_error.h

Lines changed: 23 additions & 4 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

vendor/jsontoolkit/src/json/json.cc

Lines changed: 7 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

vendor/jsontoolkit/src/uri/include/sourcemeta/jsontoolkit/uri.h

Lines changed: 17 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

vendor/jsontoolkit/src/uri/uri.cc

Lines changed: 4 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)