Skip to content

Commit 9c0727b

Browse files
authored
Improve error handling when parsing JSON from files (#1623)
Signed-off-by: Juan Cruz Viotti <jv@jviotti.com>
1 parent 0913768 commit 9c0727b

File tree

4 files changed

+25
-4
lines changed

4 files changed

+25
-4
lines changed

src/core/json/json.cc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ auto read_file(const std::filesystem::path &path)
4949
// made canonical
5050
// See https://github.com/sourcemeta/jsonschema/issues/252
5151
std::filesystem::is_fifo(path) ? path : std::filesystem::canonical(path)};
52-
stream.exceptions(std::ifstream::failbit | std::ifstream::badbit);
52+
stream.exceptions(std::ifstream::badbit);
5353
assert(!stream.fail());
5454
assert(stream.is_open());
5555
return stream;

test/json/json_parse_error_test.cc

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -200,6 +200,11 @@ TEST(JSON_parse_error, empty_object_missing_right_curly) {
200200
EXPECT_PARSE_ERROR(input, 1, 2);
201201
}
202202

203+
TEST(JSON_parse_error, object_missing_closing_curly) {
204+
std::istringstream input{"{\"foo\":{}"};
205+
EXPECT_PARSE_ERROR(input, 1, 10);
206+
}
207+
203208
TEST(JSON_parse_error, object_integer_key) {
204209
std::istringstream input{"{1:false}"};
205210
EXPECT_PARSE_ERROR(input, 1, 2);
@@ -654,13 +659,13 @@ TEST(JSON_parse_error, backspace_is_not_whitespace) {
654659
EXPECT_PARSE_ERROR(input, 1, 1);
655660
}
656661

657-
TEST(JSON_parse_error, read_json_invalid) {
662+
TEST(JSON_parse_error, read_json_invalid_1) {
658663
try {
659664
sourcemeta::core::read_json(std::filesystem::path{TEST_DIRECTORY} /
660-
"stub_invalid.json");
665+
"stub_invalid_1.json");
661666
} catch (const sourcemeta::core::JSONFileParseError &error) {
662667
EXPECT_EQ(error.path(),
663-
std::filesystem::path{TEST_DIRECTORY} / "stub_invalid.json");
668+
std::filesystem::path{TEST_DIRECTORY} / "stub_invalid_1.json");
664669
EXPECT_EQ(error.line(), 3);
665670
EXPECT_EQ(error.column(), 9);
666671
EXPECT_STREQ(error.what(), "Failed to parse the JSON document");
@@ -669,6 +674,21 @@ TEST(JSON_parse_error, read_json_invalid) {
669674
}
670675
}
671676

677+
TEST(JSON_parse_error, read_json_invalid_2) {
678+
try {
679+
sourcemeta::core::read_json(std::filesystem::path{TEST_DIRECTORY} /
680+
"stub_invalid_2.json");
681+
} catch (const sourcemeta::core::JSONFileParseError &error) {
682+
EXPECT_EQ(error.path(),
683+
std::filesystem::path{TEST_DIRECTORY} / "stub_invalid_2.json");
684+
EXPECT_EQ(error.line(), 2);
685+
EXPECT_EQ(error.column(), 1);
686+
EXPECT_STREQ(error.what(), "Failed to parse the JSON document");
687+
} catch (...) {
688+
FAIL() << "The parse function was expected to throw a file parse error";
689+
}
690+
}
691+
672692
TEST(JSON_parse_error, read_json_invalid_bigint) {
673693
try {
674694
sourcemeta::core::read_json(std::filesystem::path{TEST_DIRECTORY} /
File renamed without changes.

test/json/stub_invalid_2.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
{"foo": {}

0 commit comments

Comments
 (0)