Add better end of input error message (#4037)

This commit is contained in:
Aviral Singh 2023-07-17 18:44:10 +05:30
parent 5d2754306d
commit 63152c59aa
4 changed files with 17 additions and 9 deletions

View File

@ -479,6 +479,10 @@ class parser
error_msg += concat(m_lexer.get_error_message(), "; last read: '", error_msg += concat(m_lexer.get_error_message(), "; last read: '",
m_lexer.get_token_string(), '\''); m_lexer.get_token_string(), '\'');
} }
else if (last_token == token_type::end_of_input && m_lexer.get_position().chars_read_total == 1)
{
error_msg += concat("unexpected end of input, check that your input string or stream contains the expected JSON");
}
else else
{ {
error_msg += concat("unexpected ", lexer_t::token_type_name(last_token)); error_msg += concat("unexpected ", lexer_t::token_type_name(last_token));

View File

@ -12581,6 +12581,10 @@ class parser
error_msg += concat(m_lexer.get_error_message(), "; last read: '", error_msg += concat(m_lexer.get_error_message(), "; last read: '",
m_lexer.get_token_string(), '\''); m_lexer.get_token_string(), '\'');
} }
else if (last_token == token_type::end_of_input && m_lexer.get_position().chars_read_total == 1)
{
error_msg += concat("unexpected end of input, check that your input string or stream contains the expected JSON");
}
else else
{ {
error_msg += concat("unexpected ", lexer_t::token_type_name(last_token)); error_msg += concat("unexpected ", lexer_t::token_type_name(last_token));

View File

@ -74,7 +74,7 @@ TEST_CASE("Better diagnostics")
SECTION("Parse error") SECTION("Parse error")
{ {
json _; json _;
CHECK_THROWS_WITH_AS(_ = json::parse(""), "[json.exception.parse_error.101] parse error at line 1, column 1: syntax error while parsing value - unexpected end of input; expected '[', '{', or a literal", json::parse_error); CHECK_THROWS_WITH_AS(_ = json::parse(""), "[json.exception.parse_error.101] parse error at line 1, column 1: syntax error while parsing value - unexpected end of input, check that your input string or stream contains the expected JSON; expected '[', '{', or a literal", json::parse_error);
} }
SECTION("Wrong type in update()") SECTION("Wrong type in update()")

View File

@ -722,7 +722,7 @@ TEST_CASE("regression tests 1")
{ {
std::ifstream f("file_not_found.json"); std::ifstream f("file_not_found.json");
json _; json _;
CHECK_THROWS_WITH_AS(_ = json::parse(f), "[json.exception.parse_error.101] parse error at line 1, column 1: syntax error while parsing value - unexpected end of input; expected '[', '{', or a literal", json::parse_error&); CHECK_THROWS_WITH_AS(_ = json::parse(f), "[json.exception.parse_error.101] parse error at line 1, column 1: syntax error while parsing value - unexpected end of input, check that your input string or stream contains the expected JSON; expected '[', '{', or a literal", json::parse_error&);
} }
SECTION("issue #367 - calling stream at EOF") SECTION("issue #367 - calling stream at EOF")
@ -736,7 +736,7 @@ TEST_CASE("regression tests 1")
// ss is not at EOF; this yielded an error before the fix // ss is not at EOF; this yielded an error before the fix
// (threw basic_string::append). No, it should just throw // (threw basic_string::append). No, it should just throw
// a parse error because of the EOF. // a parse error because of the EOF.
CHECK_THROWS_WITH_AS(ss >> j, "[json.exception.parse_error.101] parse error at line 1, column 1: syntax error while parsing value - unexpected end of input; expected '[', '{', or a literal", json::parse_error&); CHECK_THROWS_WITH_AS(ss >> j, "[json.exception.parse_error.101] parse error at line 1, column 1: syntax error while parsing value - unexpected end of input, check that your input string or stream contains the expected JSON; expected '[', '{', or a literal", json::parse_error&);
} }
SECTION("issue #367 - behavior of operator>> should more closely resemble that of built-in overloads") SECTION("issue #367 - behavior of operator>> should more closely resemble that of built-in overloads")
@ -745,7 +745,7 @@ TEST_CASE("regression tests 1")
{ {
std::stringstream ss; std::stringstream ss;
json j; json j;
CHECK_THROWS_WITH_AS(ss >> j, "[json.exception.parse_error.101] parse error at line 1, column 1: syntax error while parsing value - unexpected end of input; expected '[', '{', or a literal", json::parse_error&); CHECK_THROWS_WITH_AS(ss >> j, "[json.exception.parse_error.101] parse error at line 1, column 1: syntax error while parsing value - unexpected end of input, check that your input string or stream contains the expected JSON; expected '[', '{', or a literal", json::parse_error&);
} }
SECTION("(whitespace)") SECTION("(whitespace)")
@ -765,7 +765,7 @@ TEST_CASE("regression tests 1")
CHECK_NOTHROW(ss >> j); CHECK_NOTHROW(ss >> j);
CHECK(j == 111); CHECK(j == 111);
CHECK_THROWS_WITH_AS(ss >> j, "[json.exception.parse_error.101] parse error at line 1, column 1: syntax error while parsing value - unexpected end of input; expected '[', '{', or a literal", json::parse_error&); CHECK_THROWS_WITH_AS(ss >> j, "[json.exception.parse_error.101] parse error at line 1, column 1: syntax error while parsing value - unexpected end of input, check that your input string or stream contains the expected JSON; expected '[', '{', or a literal", json::parse_error&);
} }
SECTION("one value + whitespace") SECTION("one value + whitespace")
@ -788,7 +788,7 @@ TEST_CASE("regression tests 1")
CHECK_NOTHROW(ss >> j); CHECK_NOTHROW(ss >> j);
CHECK(j == 333); CHECK(j == 333);
CHECK_THROWS_WITH_AS(ss >> j, "[json.exception.parse_error.101] parse error at line 1, column 1: syntax error while parsing value - unexpected end of input; expected '[', '{', or a literal", json::parse_error&); CHECK_THROWS_WITH_AS(ss >> j, "[json.exception.parse_error.101] parse error at line 1, column 1: syntax error while parsing value - unexpected end of input, check that your input string or stream contains the expected JSON; expected '[', '{', or a literal", json::parse_error&);
} }
SECTION("three values") SECTION("three values")
@ -803,7 +803,7 @@ TEST_CASE("regression tests 1")
CHECK_NOTHROW(ss >> j); CHECK_NOTHROW(ss >> j);
CHECK(j == 333); CHECK(j == 333);
CHECK_THROWS_WITH_AS(ss >> j, "[json.exception.parse_error.101] parse error at line 1, column 1: syntax error while parsing value - unexpected end of input; expected '[', '{', or a literal", json::parse_error&); CHECK_THROWS_WITH_AS(ss >> j, "[json.exception.parse_error.101] parse error at line 1, column 1: syntax error while parsing value - unexpected end of input, check that your input string or stream contains the expected JSON; expected '[', '{', or a literal", json::parse_error&);
} }
SECTION("literals without whitespace") SECTION("literals without whitespace")
@ -820,7 +820,7 @@ TEST_CASE("regression tests 1")
CHECK_NOTHROW(ss >> j); CHECK_NOTHROW(ss >> j);
CHECK(j == ""); CHECK(j == "");
CHECK_THROWS_WITH_AS(ss >> j, "[json.exception.parse_error.101] parse error at line 1, column 1: syntax error while parsing value - unexpected end of input; expected '[', '{', or a literal", json::parse_error&); CHECK_THROWS_WITH_AS(ss >> j, "[json.exception.parse_error.101] parse error at line 1, column 1: syntax error while parsing value - unexpected end of input, check that your input string or stream contains the expected JSON; expected '[', '{', or a literal", json::parse_error&);
} }
SECTION("example from #529") SECTION("example from #529")
@ -833,7 +833,7 @@ TEST_CASE("regression tests 1")
CHECK_NOTHROW(ss >> j); CHECK_NOTHROW(ss >> j);
CHECK(j == json({{"three", 3}})); CHECK(j == json({{"three", 3}}));
CHECK_THROWS_WITH_AS(ss >> j, "[json.exception.parse_error.101] parse error at line 1, column 1: syntax error while parsing value - unexpected end of input; expected '[', '{', or a literal", json::parse_error&); CHECK_THROWS_WITH_AS(ss >> j, "[json.exception.parse_error.101] parse error at line 1, column 1: syntax error while parsing value - unexpected end of input, check that your input string or stream contains the expected JSON; expected '[', '{', or a literal", json::parse_error&);
} }
SECTION("second example from #529") SECTION("second example from #529")