🔨 user-defined exception 302

Also moved all exception classes into the detail namespace and
introduced them via “using” into basic_json.
This commit is contained in:
Niels Lohmann 2017-03-06 19:34:44 +01:00
parent 38c2e20ce8
commit a186106bde
No known key found for this signature in database
GPG key ID: 7F3CEA63AE251B69
3 changed files with 530 additions and 462 deletions

View file

@ -78,28 +78,28 @@ TEST_CASE("value conversion")
SECTION("exception in case of a non-object type")
{
CHECK_THROWS_AS(json(json::value_t::null).get<json::object_t>(), std::logic_error);
CHECK_THROWS_AS(json(json::value_t::array).get<json::object_t>(), std::logic_error);
CHECK_THROWS_AS(json(json::value_t::string).get<json::object_t>(), std::logic_error);
CHECK_THROWS_AS(json(json::value_t::boolean).get<json::object_t>(), std::logic_error);
CHECK_THROWS_AS(json(json::value_t::number_integer).get<json::object_t>(), std::logic_error);
CHECK_THROWS_AS(json(json::value_t::number_unsigned).get<json::object_t>(), std::logic_error);
CHECK_THROWS_AS(json(json::value_t::number_float).get<json::object_t>(), std::logic_error);
CHECK_THROWS_AS(json(json::value_t::null).get<json::object_t>(), json::type_error);
CHECK_THROWS_AS(json(json::value_t::array).get<json::object_t>(), json::type_error);
CHECK_THROWS_AS(json(json::value_t::string).get<json::object_t>(), json::type_error);
CHECK_THROWS_AS(json(json::value_t::boolean).get<json::object_t>(), json::type_error);
CHECK_THROWS_AS(json(json::value_t::number_integer).get<json::object_t>(), json::type_error);
CHECK_THROWS_AS(json(json::value_t::number_unsigned).get<json::object_t>(), json::type_error);
CHECK_THROWS_AS(json(json::value_t::number_float).get<json::object_t>(), json::type_error);
CHECK_THROWS_WITH(json(json::value_t::null).get<json::object_t>(),
"type must be object, but is null");
"[json.exception.type_error.302] type must be object, but is null");
CHECK_THROWS_WITH(json(json::value_t::array).get<json::object_t>(),
"type must be object, but is array");
"[json.exception.type_error.302] type must be object, but is array");
CHECK_THROWS_WITH(json(json::value_t::string).get<json::object_t>(),
"type must be object, but is string");
"[json.exception.type_error.302] type must be object, but is string");
CHECK_THROWS_WITH(json(json::value_t::boolean).get<json::object_t>(),
"type must be object, but is boolean");
"[json.exception.type_error.302] type must be object, but is boolean");
CHECK_THROWS_WITH(json(json::value_t::number_integer).get<json::object_t>(),
"type must be object, but is number");
"[json.exception.type_error.302] type must be object, but is number");
CHECK_THROWS_WITH(json(json::value_t::number_unsigned).get<json::object_t>(),
"type must be object, but is number");
"[json.exception.type_error.302] type must be object, but is number");
CHECK_THROWS_WITH(json(json::value_t::number_float).get<json::object_t>(),
"type must be object, but is number");
"[json.exception.type_error.302] type must be object, but is number");
}
}
@ -161,9 +161,9 @@ TEST_CASE("value conversion")
std::forward_list<json> a = j.get<std::forward_list<json>>();
CHECK(json(a) == j);
CHECK_THROWS_AS(json(json::value_t::null).get<std::forward_list<json>>(), std::logic_error);
CHECK_THROWS_AS(json(json::value_t::null).get<std::forward_list<json>>(), json::type_error);
CHECK_THROWS_WITH(json(json::value_t::null).get<std::forward_list<json>>(),
"type must be array, but is null");
"[json.exception.type_error.302] type must be array, but is null");
}
SECTION("std::vector<json>")
@ -171,16 +171,16 @@ TEST_CASE("value conversion")
std::vector<json> a = j.get<std::vector<json>>();
CHECK(json(a) == j);
CHECK_THROWS_AS(json(json::value_t::null).get<std::vector<json>>(), std::logic_error);
CHECK_THROWS_AS(json(json::value_t::null).get<std::vector<json>>(), json::type_error);
CHECK_THROWS_WITH(json(json::value_t::null).get<std::vector<json>>(),
"type must be array, but is null");
"[json.exception.type_error.302] type must be array, but is null");
#if not defined(JSON_NOEXCEPTION)
SECTION("reserve is called on containers that supports it")
{
// making the call to from_json throw in order to check capacity
std::vector<float> v;
CHECK_THROWS_AS(nlohmann::from_json(j, v), std::logic_error);
CHECK_THROWS_AS(nlohmann::from_json(j, v), json::type_error);
CHECK(v.capacity() == j.size());
// make sure all values are properly copied
@ -198,30 +198,30 @@ TEST_CASE("value conversion")
SECTION("exception in case of a non-array type")
{
CHECK_THROWS_AS(json(json::value_t::null).get<json::array_t>(), std::logic_error);
CHECK_THROWS_AS(json(json::value_t::object).get<json::array_t>(), std::logic_error);
CHECK_THROWS_AS(json(json::value_t::string).get<json::array_t>(), std::logic_error);
CHECK_THROWS_AS(json(json::value_t::boolean).get<json::array_t>(), std::logic_error);
CHECK_THROWS_AS(json(json::value_t::number_integer).get<json::array_t>(), std::logic_error);
CHECK_THROWS_AS(json(json::value_t::number_unsigned).get<json::array_t>(), std::logic_error);
CHECK_THROWS_AS(json(json::value_t::number_float).get<json::array_t>(), std::logic_error);
CHECK_THROWS_AS(json(json::value_t::null).get<json::array_t>(), json::type_error);
CHECK_THROWS_AS(json(json::value_t::object).get<json::array_t>(), json::type_error);
CHECK_THROWS_AS(json(json::value_t::string).get<json::array_t>(), json::type_error);
CHECK_THROWS_AS(json(json::value_t::boolean).get<json::array_t>(), json::type_error);
CHECK_THROWS_AS(json(json::value_t::number_integer).get<json::array_t>(), json::type_error);
CHECK_THROWS_AS(json(json::value_t::number_unsigned).get<json::array_t>(), json::type_error);
CHECK_THROWS_AS(json(json::value_t::number_float).get<json::array_t>(), json::type_error);
CHECK_THROWS_WITH(json(json::value_t::object).get<std::vector<int>>(),
"type must be array, but is object");
"[json.exception.type_error.302] type must be array, but is object");
CHECK_THROWS_WITH(json(json::value_t::null).get<json::array_t>(),
"type must be array, but is null");
"[json.exception.type_error.302] type must be array, but is null");
CHECK_THROWS_WITH(json(json::value_t::object).get<json::array_t>(),
"type must be array, but is object");
"[json.exception.type_error.302] type must be array, but is object");
CHECK_THROWS_WITH(json(json::value_t::string).get<json::array_t>(),
"type must be array, but is string");
"[json.exception.type_error.302] type must be array, but is string");
CHECK_THROWS_WITH(json(json::value_t::boolean).get<json::array_t>(),
"type must be array, but is boolean");
"[json.exception.type_error.302] type must be array, but is boolean");
CHECK_THROWS_WITH(json(json::value_t::number_integer).get<json::array_t>(),
"type must be array, but is number");
"[json.exception.type_error.302] type must be array, but is number");
CHECK_THROWS_WITH(json(json::value_t::number_unsigned).get<json::array_t>(),
"type must be array, but is number");
"[json.exception.type_error.302] type must be array, but is number");
CHECK_THROWS_WITH(json(json::value_t::number_float).get<json::array_t>(),
"type must be array, but is number");
"[json.exception.type_error.302] type must be array, but is number");
}
}
@ -280,28 +280,28 @@ TEST_CASE("value conversion")
SECTION("exception in case of a non-string type")
{
CHECK_THROWS_AS(json(json::value_t::null).get<json::string_t>(), std::logic_error);
CHECK_THROWS_AS(json(json::value_t::object).get<json::string_t>(), std::logic_error);
CHECK_THROWS_AS(json(json::value_t::array).get<json::string_t>(), std::logic_error);
CHECK_THROWS_AS(json(json::value_t::boolean).get<json::string_t>(), std::logic_error);
CHECK_THROWS_AS(json(json::value_t::number_integer).get<json::string_t>(), std::logic_error);
CHECK_THROWS_AS(json(json::value_t::number_unsigned).get<json::string_t>(), std::logic_error);
CHECK_THROWS_AS(json(json::value_t::number_float).get<json::string_t>(), std::logic_error);
CHECK_THROWS_AS(json(json::value_t::null).get<json::string_t>(), json::type_error);
CHECK_THROWS_AS(json(json::value_t::object).get<json::string_t>(), json::type_error);
CHECK_THROWS_AS(json(json::value_t::array).get<json::string_t>(), json::type_error);
CHECK_THROWS_AS(json(json::value_t::boolean).get<json::string_t>(), json::type_error);
CHECK_THROWS_AS(json(json::value_t::number_integer).get<json::string_t>(), json::type_error);
CHECK_THROWS_AS(json(json::value_t::number_unsigned).get<json::string_t>(), json::type_error);
CHECK_THROWS_AS(json(json::value_t::number_float).get<json::string_t>(), json::type_error);
CHECK_THROWS_WITH(json(json::value_t::null).get<json::string_t>(),
"type must be string, but is null");
"[json.exception.type_error.302] type must be string, but is null");
CHECK_THROWS_WITH(json(json::value_t::object).get<json::string_t>(),
"type must be string, but is object");
"[json.exception.type_error.302] type must be string, but is object");
CHECK_THROWS_WITH(json(json::value_t::array).get<json::string_t>(),
"type must be string, but is array");
"[json.exception.type_error.302] type must be string, but is array");
CHECK_THROWS_WITH(json(json::value_t::boolean).get<json::string_t>(),
"type must be string, but is boolean");
"[json.exception.type_error.302] type must be string, but is boolean");
CHECK_THROWS_WITH(json(json::value_t::number_integer).get<json::string_t>(),
"type must be string, but is number");
"[json.exception.type_error.302] type must be string, but is number");
CHECK_THROWS_WITH(json(json::value_t::number_unsigned).get<json::string_t>(),
"type must be string, but is number");
"[json.exception.type_error.302] type must be string, but is number");
CHECK_THROWS_WITH(json(json::value_t::number_float).get<json::string_t>(),
"type must be string, but is number");
"[json.exception.type_error.302] type must be string, but is number");
}
}
@ -342,28 +342,28 @@ TEST_CASE("value conversion")
SECTION("exception in case of a non-string type")
{
CHECK_THROWS_AS(json(json::value_t::null).get<json::boolean_t>(), std::logic_error);
CHECK_THROWS_AS(json(json::value_t::object).get<json::boolean_t>(), std::logic_error);
CHECK_THROWS_AS(json(json::value_t::array).get<json::boolean_t>(), std::logic_error);
CHECK_THROWS_AS(json(json::value_t::string).get<json::boolean_t>(), std::logic_error);
CHECK_THROWS_AS(json(json::value_t::number_integer).get<json::boolean_t>(), std::logic_error);
CHECK_THROWS_AS(json(json::value_t::number_unsigned).get<json::boolean_t>(), std::logic_error);
CHECK_THROWS_AS(json(json::value_t::number_float).get<json::boolean_t>(), std::logic_error);
CHECK_THROWS_AS(json(json::value_t::null).get<json::boolean_t>(), json::type_error);
CHECK_THROWS_AS(json(json::value_t::object).get<json::boolean_t>(), json::type_error);
CHECK_THROWS_AS(json(json::value_t::array).get<json::boolean_t>(), json::type_error);
CHECK_THROWS_AS(json(json::value_t::string).get<json::boolean_t>(), json::type_error);
CHECK_THROWS_AS(json(json::value_t::number_integer).get<json::boolean_t>(), json::type_error);
CHECK_THROWS_AS(json(json::value_t::number_unsigned).get<json::boolean_t>(), json::type_error);
CHECK_THROWS_AS(json(json::value_t::number_float).get<json::boolean_t>(), json::type_error);
CHECK_THROWS_WITH(json(json::value_t::null).get<json::boolean_t>(),
"type must be boolean, but is null");
"[json.exception.type_error.302] type must be boolean, but is null");
CHECK_THROWS_WITH(json(json::value_t::object).get<json::boolean_t>(),
"type must be boolean, but is object");
"[json.exception.type_error.302] type must be boolean, but is object");
CHECK_THROWS_WITH(json(json::value_t::array).get<json::boolean_t>(),
"type must be boolean, but is array");
"[json.exception.type_error.302] type must be boolean, but is array");
CHECK_THROWS_WITH(json(json::value_t::string).get<json::boolean_t>(),
"type must be boolean, but is string");
"[json.exception.type_error.302] type must be boolean, but is string");
CHECK_THROWS_WITH(json(json::value_t::number_integer).get<json::boolean_t>(),
"type must be boolean, but is number");
"[json.exception.type_error.302] type must be boolean, but is number");
CHECK_THROWS_WITH(json(json::value_t::number_unsigned).get<json::boolean_t>(),
"type must be boolean, but is number");
"[json.exception.type_error.302] type must be boolean, but is number");
CHECK_THROWS_WITH(json(json::value_t::number_float).get<json::boolean_t>(),
"type must be boolean, but is number");
"[json.exception.type_error.302] type must be boolean, but is number");
}
}
@ -598,22 +598,22 @@ TEST_CASE("value conversion")
SECTION("exception in case of a non-number type")
{
CHECK_THROWS_AS(json(json::value_t::null).get<json::number_integer_t>(), std::logic_error);
CHECK_THROWS_AS(json(json::value_t::object).get<json::number_integer_t>(), std::logic_error);
CHECK_THROWS_AS(json(json::value_t::array).get<json::number_integer_t>(), std::logic_error);
CHECK_THROWS_AS(json(json::value_t::string).get<json::number_integer_t>(), std::logic_error);
CHECK_THROWS_AS(json(json::value_t::boolean).get<json::number_integer_t>(), std::logic_error);
CHECK_THROWS_AS(json(json::value_t::null).get<json::number_integer_t>(), json::type_error);
CHECK_THROWS_AS(json(json::value_t::object).get<json::number_integer_t>(), json::type_error);
CHECK_THROWS_AS(json(json::value_t::array).get<json::number_integer_t>(), json::type_error);
CHECK_THROWS_AS(json(json::value_t::string).get<json::number_integer_t>(), json::type_error);
CHECK_THROWS_AS(json(json::value_t::boolean).get<json::number_integer_t>(), json::type_error);
CHECK_THROWS_WITH(json(json::value_t::null).get<json::number_integer_t>(),
"type must be number, but is null");
"[json.exception.type_error.302] type must be number, but is null");
CHECK_THROWS_WITH(json(json::value_t::object).get<json::number_integer_t>(),
"type must be number, but is object");
"[json.exception.type_error.302] type must be number, but is object");
CHECK_THROWS_WITH(json(json::value_t::array).get<json::number_integer_t>(),
"type must be number, but is array");
"[json.exception.type_error.302] type must be number, but is array");
CHECK_THROWS_WITH(json(json::value_t::string).get<json::number_integer_t>(),
"type must be number, but is string");
"[json.exception.type_error.302] type must be number, but is string");
CHECK_THROWS_WITH(json(json::value_t::boolean).get<json::number_integer_t>(),
"type must be number, but is boolean");
"[json.exception.type_error.302] type must be number, but is boolean");
CHECK_NOTHROW(json(json::value_t::number_float).get<json::number_integer_t>());
CHECK_NOTHROW(json(json::value_t::number_float).get<json::number_unsigned_t>());
@ -857,22 +857,22 @@ TEST_CASE("value conversion")
SECTION("exception in case of a non-string type")
{
CHECK_THROWS_AS(json(json::value_t::null).get<json::number_float_t>(), std::logic_error);
CHECK_THROWS_AS(json(json::value_t::object).get<json::number_float_t>(), std::logic_error);
CHECK_THROWS_AS(json(json::value_t::array).get<json::number_float_t>(), std::logic_error);
CHECK_THROWS_AS(json(json::value_t::string).get<json::number_float_t>(), std::logic_error);
CHECK_THROWS_AS(json(json::value_t::boolean).get<json::number_float_t>(), std::logic_error);
CHECK_THROWS_AS(json(json::value_t::null).get<json::number_float_t>(), json::type_error);
CHECK_THROWS_AS(json(json::value_t::object).get<json::number_float_t>(), json::type_error);
CHECK_THROWS_AS(json(json::value_t::array).get<json::number_float_t>(), json::type_error);
CHECK_THROWS_AS(json(json::value_t::string).get<json::number_float_t>(), json::type_error);
CHECK_THROWS_AS(json(json::value_t::boolean).get<json::number_float_t>(), json::type_error);
CHECK_THROWS_WITH(json(json::value_t::null).get<json::number_float_t>(),
"type must be number, but is null");
"[json.exception.type_error.302] type must be number, but is null");
CHECK_THROWS_WITH(json(json::value_t::object).get<json::number_float_t>(),
"type must be number, but is object");
"[json.exception.type_error.302] type must be number, but is object");
CHECK_THROWS_WITH(json(json::value_t::array).get<json::number_float_t>(),
"type must be number, but is array");
"[json.exception.type_error.302] type must be number, but is array");
CHECK_THROWS_WITH(json(json::value_t::string).get<json::number_float_t>(),
"type must be number, but is string");
"[json.exception.type_error.302] type must be number, but is string");
CHECK_THROWS_WITH(json(json::value_t::boolean).get<json::number_float_t>(),
"type must be number, but is boolean");
"[json.exception.type_error.302] type must be number, but is boolean");
CHECK_NOTHROW(json(json::value_t::number_integer).get<json::number_float_t>());
CHECK_NOTHROW(json(json::value_t::number_unsigned).get<json::number_float_t>());
@ -954,8 +954,8 @@ TEST_CASE("value conversion")
SECTION("exception in case of a non-object type")
{
CHECK_THROWS_AS((json().get<std::map<std::string, int>>()), std::logic_error);
CHECK_THROWS_WITH((json().get<std::map<std::string, int>>()), "type must be object, but is null");
CHECK_THROWS_AS((json().get<std::map<std::string, int>>()), json::type_error);
CHECK_THROWS_WITH((json().get<std::map<std::string, int>>()), "[json.exception.type_error.302] type must be object, but is null");
}
}
@ -1023,17 +1023,17 @@ TEST_CASE("value conversion")
SECTION("exception in case of a non-object type")
{
CHECK_THROWS_AS((json().get<std::list<int>>()), std::logic_error);
CHECK_THROWS_AS((json().get<std::vector<int>>()), std::logic_error);
CHECK_THROWS_AS((json().get<std::vector<json>>()), std::logic_error);
CHECK_THROWS_AS((json().get<std::list<json>>()), std::logic_error);
CHECK_THROWS_AS((json().get<std::list<int>>()), json::type_error);
CHECK_THROWS_AS((json().get<std::vector<int>>()), json::type_error);
CHECK_THROWS_AS((json().get<std::vector<json>>()), json::type_error);
CHECK_THROWS_AS((json().get<std::list<json>>()), json::type_error);
// does type really must be an array? or it rather must not be null?
// that's what I thought when other test like this one broke
CHECK_THROWS_WITH((json().get<std::list<int>>()), "type must be array, but is null");
CHECK_THROWS_WITH((json().get<std::vector<int>>()), "type must be array, but is null");
CHECK_THROWS_WITH((json().get<std::vector<json>>()), "type must be array, but is null");
CHECK_THROWS_WITH((json().get<std::list<json>>()), "type must be array, but is null");
CHECK_THROWS_WITH((json().get<std::list<int>>()), "[json.exception.type_error.302] type must be array, but is null");
CHECK_THROWS_WITH((json().get<std::vector<int>>()), "[json.exception.type_error.302] type must be array, but is null");
CHECK_THROWS_WITH((json().get<std::vector<json>>()), "[json.exception.type_error.302] type must be array, but is null");
CHECK_THROWS_WITH((json().get<std::list<json>>()), "[json.exception.type_error.302] type must be array, but is null");
}
}
}