Skip to content

Commit

Permalink
🔨 adjust exceptions
Browse files Browse the repository at this point in the history
  • Loading branch information
nlohmann committed Oct 10, 2021
1 parent 9fba127 commit 3294bfe
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 14 deletions.
10 changes: 4 additions & 6 deletions include/nlohmann/json.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -6019,9 +6019,8 @@ class basic_json // NOLINT(cppcoreguidelines-special-member-functions,hicpp-spec
@throw type_error.312 if called on JSON values other than objects; example:
`"cannot use update() with string"`
@throw invalid_iterator.202 if iterator @a first or @a last does does not
point to an object; example: `"iterators first and last must point to
objects"`
@throw type_error.312 if iterator @a first or @a last does does not
point to an object; example: `"cannot use update() with string"`
@throw invalid_iterator.210 if @a first and @a last do not belong to the
same JSON value; example: `"iterators do not fit"`
Expand Down Expand Up @@ -6056,10 +6055,9 @@ class basic_json // NOLINT(cppcoreguidelines-special-member-functions,hicpp-spec
}

// passed iterators must belong to objects
if (JSON_HEDLEY_UNLIKELY(!first.m_object->is_object()
|| !last.m_object->is_object()))
if (JSON_HEDLEY_UNLIKELY(!first.m_object->is_object()))
{
JSON_THROW(invalid_iterator::create(202, "iterators first and last must point to objects", *this));
JSON_THROW(type_error::create(312, "cannot use update() with " + std::string(first.m_object->type_name()), *first.m_object));
}

for (auto it = first; it != last; ++it)
Expand Down
10 changes: 4 additions & 6 deletions single_include/nlohmann/json.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -23506,9 +23506,8 @@ class basic_json // NOLINT(cppcoreguidelines-special-member-functions,hicpp-spec

@throw type_error.312 if called on JSON values other than objects; example:
`"cannot use update() with string"`
@throw invalid_iterator.202 if iterator @a first or @a last does does not
point to an object; example: `"iterators first and last must point to
objects"`
@throw type_error.312 if iterator @a first or @a last does does not
point to an object; example: `"cannot use update() with string"`
@throw invalid_iterator.210 if @a first and @a last do not belong to the
same JSON value; example: `"iterators do not fit"`

Expand Down Expand Up @@ -23543,10 +23542,9 @@ class basic_json // NOLINT(cppcoreguidelines-special-member-functions,hicpp-spec
}

// passed iterators must belong to objects
if (JSON_HEDLEY_UNLIKELY(!first.m_object->is_object()
|| !last.m_object->is_object()))
if (JSON_HEDLEY_UNLIKELY(!first.m_object->is_object()))
{
JSON_THROW(invalid_iterator::create(202, "iterators first and last must point to objects", *this));
JSON_THROW(type_error::create(312, "cannot use update() with " + std::string(first.m_object->type_name()), *first.m_object));
}

for (auto it = first; it != last; ++it)
Expand Down
9 changes: 9 additions & 0 deletions test/src/unit-diagnostics.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,15 @@ TEST_CASE("Better diagnostics")
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);
}

SECTION("Wrong type in update()")
{
json j = {{"foo", "bar"}};
json k = {{"bla", 1}};

CHECK_THROWS_WITH_AS(j.update(k["bla"].begin(), k["bla"].end()), "[json.exception.type_error.312] (/bla) cannot use update() with number", json::type_error);
CHECK_THROWS_WITH_AS(j.update(k["bla"]), "[json.exception.type_error.312] (/bla) cannot use update() with number", json::type_error);
}
}

TEST_CASE("Regression tests for extended diagnostics")
Expand Down
4 changes: 2 additions & 2 deletions test/src/unit-modifiers.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -848,14 +848,14 @@ TEST_CASE("modifiers")

CHECK_THROWS_AS(j_array.update(j_object2.begin(), j_object2.end()), json::type_error&);
CHECK_THROWS_AS(j_object1.update(j_object1.begin(), j_object2.end()), json::invalid_iterator&);
CHECK_THROWS_AS(j_object1.update(j_array.begin(), j_array.end()), json::invalid_iterator&);
CHECK_THROWS_AS(j_object1.update(j_array.begin(), j_array.end()), json::type_error&);

CHECK_THROWS_WITH(j_array.update(j_object2.begin(), j_object2.end()),
"[json.exception.type_error.312] cannot use update() with array");
CHECK_THROWS_WITH(j_object1.update(j_object1.begin(), j_object2.end()),
"[json.exception.invalid_iterator.210] iterators do not fit");
CHECK_THROWS_WITH(j_object1.update(j_array.begin(), j_array.end()),
"[json.exception.invalid_iterator.202] iterators first and last must point to objects");
"[json.exception.type_error.312] cannot use update() with array");
}
}
}
Expand Down

0 comments on commit 3294bfe

Please sign in to comment.