Skip to content

Commit

Permalink
Print nicer messages when encountering JSON parsing errors (#88)
Browse files Browse the repository at this point in the history
Signed-off-by: Juan Cruz Viotti <[email protected]>
  • Loading branch information
jviotti authored Jun 13, 2024
1 parent 9bd84cd commit 9289d16
Show file tree
Hide file tree
Showing 9 changed files with 94 additions and 6 deletions.
2 changes: 1 addition & 1 deletion DEPENDENCIES
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
vendorpull https://github.com/sourcemeta/vendorpull dea311b5bfb53b6926a4140267959ae334d3ecf4
noa https://github.com/sourcemeta/noa 2bc3138b80e575786bec418c91fc2058c6836993
jsontoolkit https://github.com/sourcemeta/jsontoolkit 37580da97c6549cd5995b3ef62ad47892246ebfd
jsontoolkit https://github.com/sourcemeta/jsontoolkit 1d291fc26bae59e778067e5dfa54096a1fb608f3
hydra https://github.com/sourcemeta/hydra 3c53d3fdef79e9ba603d48470a508cc45472a0dc
8 changes: 8 additions & 0 deletions src/main.cc
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,14 @@ auto main(int argc, char *argv[]) noexcept -> int {
} catch (const sourcemeta::jsontoolkit::SchemaResolutionError &error) {
std::cerr << error.what() << ": " << error.id() << "\n";
return EXIT_FAILURE;
} catch (const sourcemeta::jsontoolkit::FileParseError &error) {
std::cerr << error.path().string() << "\n " << error.what() << " at line "
<< error.line() << " and column " << error.column() << "\n";
return EXIT_FAILURE;
} catch (const sourcemeta::jsontoolkit::ParseError &error) {
std::cerr << error.what() << " at line " << error.line() << " and column "
<< error.column() << "\n";
return EXIT_FAILURE;
} catch (const std::exception &error) {
std::cerr << "Error: " << error.what() << "\n";
return EXIT_FAILURE;
Expand Down
2 changes: 2 additions & 0 deletions src/utils.cc
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ auto handle_json_entry(
return prefix == canonical ||
path_starts_with(canonical, prefix);
})) {
// TODO: Print a verbose message for what is getting parsed
result.emplace_back(canonical,
sourcemeta::jsontoolkit::from_file(canonical));
}
Expand All @@ -74,6 +75,7 @@ auto handle_json_entry(
return prefix == canonical ||
path_starts_with(canonical, prefix);
})) {
// TODO: Print a verbose message for what is getting parsed
result.emplace_back(canonical,
sourcemeta::jsontoolkit::from_file(canonical));
}
Expand Down
1 change: 1 addition & 0 deletions test/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ add_jsonschema_test_unix(format_check_single_fail)
add_jsonschema_test_unix(format_check_single_pass)
add_jsonschema_test_unix(format_directory_ignore_directory)
add_jsonschema_test_unix(format_directory_ignore_file)
add_jsonschema_test_unix(format_check_single_invalid)
add_jsonschema_test_unix(frame)
add_jsonschema_test_unix(validate_pass_draft4)
add_jsonschema_test_unix(validate_fail_draft4)
Expand Down
31 changes: 31 additions & 0 deletions test/format_check_single_invalid.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
#!/bin/sh

set -o errexit
set -o nounset

TMP="$(mktemp -d)"
clean() { rm -rf "$TMP"; }
trap clean EXIT

cat << 'EOF' > "$TMP/schema.json"
{
"$schema": "http://json-schema.org/draft-04/schema#"
"type" 1,
}
EOF

"$1" fmt "$TMP/schema.json" --check 2>"$TMP/output.txt" && CODE="$?" || CODE="$?"

if [ "$CODE" = "0" ]
then
echo "FAIL" 1>&2
exit 1
fi

cat << EOF > "$TMP/expected.txt"
$(realpath "$TMP/schema.json")
Failed to parse the JSON document at line 3 and column 3
EOF

diff "$TMP/output.txt" "$TMP/expected.txt"
echo "PASS" 1>&2

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 7 additions & 1 deletion vendor/jsontoolkit/src/json/json.cc

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

17 changes: 17 additions & 0 deletions vendor/jsontoolkit/src/uri/include/sourcemeta/jsontoolkit/uri.h

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 4 additions & 0 deletions vendor/jsontoolkit/src/uri/uri.cc

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 9289d16

Please sign in to comment.