From 3294bfe9fa530d79f0079d92daeb73a4c8427db1 Mon Sep 17 00:00:00 2001 From: Niels Lohmann Date: Sun, 10 Oct 2021 11:02:12 +0200 Subject: [PATCH] :hammer: adjust exceptions --- include/nlohmann/json.hpp | 10 ++++------ single_include/nlohmann/json.hpp | 10 ++++------ test/src/unit-diagnostics.cpp | 9 +++++++++ test/src/unit-modifiers.cpp | 4 ++-- 4 files changed, 19 insertions(+), 14 deletions(-) diff --git a/include/nlohmann/json.hpp b/include/nlohmann/json.hpp index 9852b18c85..48748cd589 100644 --- a/include/nlohmann/json.hpp +++ b/include/nlohmann/json.hpp @@ -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"` @@ -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) diff --git a/single_include/nlohmann/json.hpp b/single_include/nlohmann/json.hpp index bf7212b4da..9d1aa0d4fc 100644 --- a/single_include/nlohmann/json.hpp +++ b/single_include/nlohmann/json.hpp @@ -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"` @@ -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) diff --git a/test/src/unit-diagnostics.cpp b/test/src/unit-diagnostics.cpp index 98d02252e9..87d4eb370f 100644 --- a/test/src/unit-diagnostics.cpp +++ b/test/src/unit-diagnostics.cpp @@ -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") diff --git a/test/src/unit-modifiers.cpp b/test/src/unit-modifiers.cpp index d6649ad806..e8fe427c63 100644 --- a/test/src/unit-modifiers.cpp +++ b/test/src/unit-modifiers.cpp @@ -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"); } } }