Skip to content

Commit

Permalink
🚧 removed the last std exceptions (#244)
Browse files Browse the repository at this point in the history
  • Loading branch information
nlohmann committed Nov 16, 2016
1 parent ddefb9b commit 7ef39db
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 24 deletions.
14 changes: 8 additions & 6 deletions src/json.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ SOFTWARE.
#include <memory> // addressof, allocator, allocator_traits, unique_ptr
#include <numeric> // accumulate
#include <sstream> // stringstream
#include <stdexcept> // domain_error, out_of_range
#include <stdexcept> // out_of_range
#include <string> // getline, stoi, string, to_string
#include <type_traits> // add_pointer, enable_if, is_arithmetic, is_base_of, is_const, is_constructible, is_convertible, is_floating_point, is_integral, is_nothrow_move_assignable, std::is_nothrow_move_constructible, std::is_pointer, std::is_reference, std::is_same, remove_const, remove_pointer, remove_reference
#include <utility> // declval, forward, make_pair, move, pair, swap
Expand Down Expand Up @@ -292,6 +292,8 @@ class basic_json
json.exception.[out_of_range](@ref out_of_range).402 | "array index '-' (3) is out of range" | ...
json.exception.[out_of_range](@ref out_of_range).403 | "key 'foo' not found" | ...
json.exception.[out_of_range](@ref out_of_range).404 | "unresolved reference token 'foo'" | ...
json.exception.[out_of_range](@ref out_of_range).405 | "JSON pointer has no parent" | The JSON Patch operations 'remove' and 'add' can not be applied to the root element of the JSON value.
json.exception.other.500 | "unsuccessful" | A JSON Patch operation 'test' failed.
@since version 3.0.0
*/
Expand Down Expand Up @@ -1960,7 +1962,7 @@ class basic_json
copied. Otherwise, out_of_range is thrown.
- In case of structured types (array, object), the constructor behaves as
similar versions for `std::vector`.
- In case of a null type, std::domain_error is thrown.
- In case of a null type, @ref invalid_iterator is thrown.
@tparam InputIT an input iterator type (@ref iterator or @ref
const_iterator)
Expand Down Expand Up @@ -9698,7 +9700,7 @@ class basic_json
{
if (is_root())
{
JSON_THROW(std::domain_error("JSON pointer has no parent"));
JSON_THROW(out_of_range(405, "JSON pointer has no parent"));
}

auto last = reference_tokens.back();
Expand All @@ -9716,7 +9718,7 @@ class basic_json
{
if (is_root())
{
JSON_THROW(std::domain_error("JSON pointer has no parent"));
JSON_THROW(out_of_range(405, "JSON pointer has no parent"));
}

json_pointer result = *this;
Expand Down Expand Up @@ -10686,10 +10688,10 @@ class basic_json
// ignore out of range errors: success remains false
}

// JSON_THROW(an exception if test fails);
// throw an exception if test fails
if (not success)
{
JSON_THROW(std::domain_error("unsuccessful: " + val.dump()));
JSON_THROW(exception(500, "other", "unsuccessful: " + val.dump()));
}

break;
Expand Down
14 changes: 8 additions & 6 deletions src/json.hpp.re2c
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ SOFTWARE.
#include <memory> // addressof, allocator, allocator_traits, unique_ptr
#include <numeric> // accumulate
#include <sstream> // stringstream
#include <stdexcept> // domain_error, out_of_range
#include <stdexcept> // out_of_range
#include <string> // getline, stoi, string, to_string
#include <type_traits> // add_pointer, enable_if, is_arithmetic, is_base_of, is_const, is_constructible, is_convertible, is_floating_point, is_integral, is_nothrow_move_assignable, std::is_nothrow_move_constructible, std::is_pointer, std::is_reference, std::is_same, remove_const, remove_pointer, remove_reference
#include <utility> // declval, forward, make_pair, move, pair, swap
Expand Down Expand Up @@ -292,6 +292,8 @@ class basic_json
json.exception.[out_of_range](@ref out_of_range).402 | "array index '-' (3) is out of range" | ...
json.exception.[out_of_range](@ref out_of_range).403 | "key 'foo' not found" | ...
json.exception.[out_of_range](@ref out_of_range).404 | "unresolved reference token 'foo'" | ...
json.exception.[out_of_range](@ref out_of_range).405 | "JSON pointer has no parent" | The JSON Patch operations 'remove' and 'add' can not be applied to the root element of the JSON value.
json.exception.other.500 | "unsuccessful" | A JSON Patch operation 'test' failed.

@since version 3.0.0
*/
Expand Down Expand Up @@ -1960,7 +1962,7 @@ class basic_json
copied. Otherwise, out_of_range is thrown.
- In case of structured types (array, object), the constructor behaves as
similar versions for `std::vector`.
- In case of a null type, std::domain_error is thrown.
- In case of a null type, @ref invalid_iterator is thrown.

@tparam InputIT an input iterator type (@ref iterator or @ref
const_iterator)
Expand Down Expand Up @@ -8846,7 +8848,7 @@ class basic_json
{
if (is_root())
{
JSON_THROW(std::domain_error("JSON pointer has no parent"));
JSON_THROW(out_of_range(405, "JSON pointer has no parent"));
}

auto last = reference_tokens.back();
Expand All @@ -8864,7 +8866,7 @@ class basic_json
{
if (is_root())
{
JSON_THROW(std::domain_error("JSON pointer has no parent"));
JSON_THROW(out_of_range(405, "JSON pointer has no parent"));
}

json_pointer result = *this;
Expand Down Expand Up @@ -9834,10 +9836,10 @@ class basic_json
// ignore out of range errors: success remains false
}

// JSON_THROW(an exception if test fails);
// throw an exception if test fails
if (not success)
{
JSON_THROW(std::domain_error("unsuccessful: " + val.dump()));
JSON_THROW(exception(500, "other", "unsuccessful: " + val.dump()));
}

break;
Expand Down
17 changes: 9 additions & 8 deletions test/src/unit-json_patch.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -336,8 +336,8 @@ TEST_CASE("JSON patch")
)"_json;

// check that evaluation throws
CHECK_THROWS_AS(doc.patch(patch), std::domain_error);
CHECK_THROWS_WITH(doc.patch(patch), "unsuccessful: " + patch[0].dump());
CHECK_THROWS_AS(doc.patch(patch), json::exception);
CHECK_THROWS_WITH(doc.patch(patch), "[json.exception.other.500] unsuccessful: " + patch[0].dump());
}

SECTION("A.10. Adding a Nested Member Object")
Expand Down Expand Up @@ -476,8 +476,8 @@ TEST_CASE("JSON patch")
)"_json;

// check that evaluation throws
CHECK_THROWS_AS(doc.patch(patch), std::domain_error);
CHECK_THROWS_WITH(doc.patch(patch), "unsuccessful: " + patch[0].dump());
CHECK_THROWS_AS(doc.patch(patch), json::exception);
CHECK_THROWS_WITH(doc.patch(patch), "[json.exception.other.500] unsuccessful: " + patch[0].dump());
}

SECTION("A.16. Adding an Array Value")
Expand Down Expand Up @@ -788,8 +788,9 @@ TEST_CASE("JSON patch")
{
json j = "string";
json patch = {{{"op", "remove"}, {"path", ""}}};
CHECK_THROWS_AS(j.patch(patch), std::domain_error);
CHECK_THROWS_WITH(j.patch(patch), "JSON pointer has no parent");
CHECK_THROWS_AS(j.patch(patch), json::out_of_range);
CHECK_THROWS_WITH(j.patch(patch),
"[json.exception.out_of_range.405] JSON pointer has no parent");
}
}

Expand Down Expand Up @@ -1170,8 +1171,8 @@ TEST_CASE("JSON patch")
)"_json;

// the test will fail
CHECK_THROWS_AS(doc.patch(patch), std::domain_error);
CHECK_THROWS_WITH(doc.patch(patch), "unsuccessful: " + patch[0].dump());
CHECK_THROWS_AS(doc.patch(patch), json::exception);
CHECK_THROWS_WITH(doc.patch(patch), "[json.exception.other.500] unsuccessful: " + patch[0].dump());
}
}
}
Expand Down
10 changes: 6 additions & 4 deletions test/src/unit-json_pointer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -49,10 +49,12 @@ TEST_CASE("JSON pointers")
"[json.exception.parse_error.108] parse error: escape character '~' must be followed with '0' or '1'");

json::json_pointer p;
CHECK_THROWS_AS(p.top(), std::domain_error);
CHECK_THROWS_WITH(p.top(), "JSON pointer has no parent");
CHECK_THROWS_AS(p.pop_back(), std::domain_error);
CHECK_THROWS_WITH(p.pop_back(), "JSON pointer has no parent");
CHECK_THROWS_AS(p.top(), json::out_of_range);
CHECK_THROWS_WITH(p.top(),
"[json.exception.out_of_range.405] JSON pointer has no parent");
CHECK_THROWS_AS(p.pop_back(), json::out_of_range);
CHECK_THROWS_WITH(p.pop_back(),
"[json.exception.out_of_range.405] JSON pointer has no parent");
}

SECTION("examples from RFC 6901")
Expand Down

0 comments on commit 7ef39db

Please sign in to comment.