🔨 added user-defined exception 403

This commit is contained in:
Niels Lohmann 2017-03-05 23:16:17 +01:00
parent 60da36aee2
commit 30331fa21f
No known key found for this signature in database
GPG key ID: 7F3CEA63AE251B69
5 changed files with 51 additions and 42 deletions

View file

@ -3734,7 +3734,7 @@ class basic_json
@throw type_error.304 if the JSON value is not an object; example: @throw type_error.304 if the JSON value is not an object; example:
`"cannot use at() with boolean"` `"cannot use at() with boolean"`
@throw std::out_of_range if the key @a key is is not stored in the object; @throw out_of_range.403 if the key @a key is is not stored in the object;
that is, `find(key) == end()`; example: `"key "the fast" not found"` that is, `find(key) == end()`; example: `"key "the fast" not found"`
@complexity Logarithmic in the size of the container. @complexity Logarithmic in the size of the container.
@ -3760,7 +3760,7 @@ class basic_json
JSON_CATCH (std::out_of_range&) JSON_CATCH (std::out_of_range&)
{ {
// create better exception explanation // create better exception explanation
JSON_THROW(std::out_of_range("key '" + key + "' not found")); JSON_THROW(out_of_range(403, "key '" + key + "' not found"));
} }
} }
else else
@ -3781,7 +3781,7 @@ class basic_json
@throw type_error.304 if the JSON value is not an object; example: @throw type_error.304 if the JSON value is not an object; example:
`"cannot use at() with boolean"` `"cannot use at() with boolean"`
@throw std::out_of_range if the key @a key is is not stored in the object; @throw out_of_range.403 if the key @a key is is not stored in the object;
that is, `find(key) == end()`; example: `"key "the fast" not found"` that is, `find(key) == end()`; example: `"key "the fast" not found"`
@complexity Logarithmic in the size of the container. @complexity Logarithmic in the size of the container.
@ -3807,7 +3807,7 @@ class basic_json
JSON_CATCH (std::out_of_range&) JSON_CATCH (std::out_of_range&)
{ {
// create better exception explanation // create better exception explanation
JSON_THROW(std::out_of_range("key '" + key + "' not found")); JSON_THROW(out_of_range(403, "key '" + key + "' not found"));
} }
} }
else else
@ -4154,7 +4154,7 @@ class basic_json
@code {.cpp} @code {.cpp}
try { try {
return at(key); return at(key);
} catch(std::out_of_range) { } catch(out_of_range) {
return default_value; return default_value;
} }
@endcode @endcode
@ -4233,7 +4233,7 @@ class basic_json
@code {.cpp} @code {.cpp}
try { try {
return at(ptr); return at(ptr);
} catch(std::out_of_range) { } catch(out_of_range) {
return default_value; return default_value;
} }
@endcode @endcode
@ -4276,7 +4276,7 @@ class basic_json
{ {
return ptr.get_checked(this); return ptr.get_checked(this);
} }
JSON_CATCH (std::out_of_range&) JSON_CATCH (out_of_range&)
{ {
return default_value; return default_value;
} }
@ -12746,9 +12746,9 @@ basic_json_parser_74:
any case, the original value is not changed: the patch is applied any case, the original value is not changed: the patch is applied
to a copy of the value. to a copy of the value.
@throw std::out_of_range if a JSON pointer inside the patch could not @throw out_of_range.403 if a JSON pointer inside the patch could not be
be resolved successfully in the current JSON value; example: `"key baz resolved successfully in the current JSON value; example: `"key baz not
not found"` found"`
@throw invalid_argument if the JSON patch is malformed (e.g., mandatory @throw invalid_argument if the JSON patch is malformed (e.g., mandatory
attributes are missing); example: `"operation add must have member path"` attributes are missing); example: `"operation add must have member path"`
@throw parse_error.104 if the JSON patch does not consist of an array of @throw parse_error.104 if the JSON patch does not consist of an array of
@ -12888,7 +12888,7 @@ basic_json_parser_74:
} }
else else
{ {
JSON_THROW(std::out_of_range("key '" + last_path + "' not found")); JSON_THROW(out_of_range(403, "key '" + last_path + "' not found"));
} }
} }
else if (parent.is_array()) else if (parent.is_array())
@ -13002,7 +13002,7 @@ basic_json_parser_74:
// the "path" location must exist - use at() // the "path" location must exist - use at()
success = (result.at(ptr) == get_value("test", "value", false)); success = (result.at(ptr) == get_value("test", "value", false));
} }
JSON_CATCH (std::out_of_range&) JSON_CATCH (out_of_range&)
{ {
// ignore out of range errors: success remains false // ignore out of range errors: success remains false
} }

View file

@ -3734,7 +3734,7 @@ class basic_json
@throw type_error.304 if the JSON value is not an object; example: @throw type_error.304 if the JSON value is not an object; example:
`"cannot use at() with boolean"` `"cannot use at() with boolean"`
@throw std::out_of_range if the key @a key is is not stored in the object; @throw out_of_range.403 if the key @a key is is not stored in the object;
that is, `find(key) == end()`; example: `"key "the fast" not found"` that is, `find(key) == end()`; example: `"key "the fast" not found"`
@complexity Logarithmic in the size of the container. @complexity Logarithmic in the size of the container.
@ -3760,7 +3760,7 @@ class basic_json
JSON_CATCH (std::out_of_range&) JSON_CATCH (std::out_of_range&)
{ {
// create better exception explanation // create better exception explanation
JSON_THROW(std::out_of_range("key '" + key + "' not found")); JSON_THROW(out_of_range(403, "key '" + key + "' not found"));
} }
} }
else else
@ -3781,7 +3781,7 @@ class basic_json
@throw type_error.304 if the JSON value is not an object; example: @throw type_error.304 if the JSON value is not an object; example:
`"cannot use at() with boolean"` `"cannot use at() with boolean"`
@throw std::out_of_range if the key @a key is is not stored in the object; @throw out_of_range.403 if the key @a key is is not stored in the object;
that is, `find(key) == end()`; example: `"key "the fast" not found"` that is, `find(key) == end()`; example: `"key "the fast" not found"`
@complexity Logarithmic in the size of the container. @complexity Logarithmic in the size of the container.
@ -3807,7 +3807,7 @@ class basic_json
JSON_CATCH (std::out_of_range&) JSON_CATCH (std::out_of_range&)
{ {
// create better exception explanation // create better exception explanation
JSON_THROW(std::out_of_range("key '" + key + "' not found")); JSON_THROW(out_of_range(403, "key '" + key + "' not found"));
} }
} }
else else
@ -4154,7 +4154,7 @@ class basic_json
@code {.cpp} @code {.cpp}
try { try {
return at(key); return at(key);
} catch(std::out_of_range) { } catch(out_of_range) {
return default_value; return default_value;
} }
@endcode @endcode
@ -4233,7 +4233,7 @@ class basic_json
@code {.cpp} @code {.cpp}
try { try {
return at(ptr); return at(ptr);
} catch(std::out_of_range) { } catch(out_of_range) {
return default_value; return default_value;
} }
@endcode @endcode
@ -4276,7 +4276,7 @@ class basic_json
{ {
return ptr.get_checked(this); return ptr.get_checked(this);
} }
JSON_CATCH (std::out_of_range&) JSON_CATCH (out_of_range&)
{ {
return default_value; return default_value;
} }
@ -11779,9 +11779,9 @@ class basic_json
any case, the original value is not changed: the patch is applied any case, the original value is not changed: the patch is applied
to a copy of the value. to a copy of the value.
@throw std::out_of_range if a JSON pointer inside the patch could not @throw out_of_range.403 if a JSON pointer inside the patch could not be
be resolved successfully in the current JSON value; example: `"key baz resolved successfully in the current JSON value; example: `"key baz not
not found"` found"`
@throw invalid_argument if the JSON patch is malformed (e.g., mandatory @throw invalid_argument if the JSON patch is malformed (e.g., mandatory
attributes are missing); example: `"operation add must have member path"` attributes are missing); example: `"operation add must have member path"`
@throw parse_error.104 if the JSON patch does not consist of an array of @throw parse_error.104 if the JSON patch does not consist of an array of
@ -11921,7 +11921,7 @@ class basic_json
} }
else else
{ {
JSON_THROW(std::out_of_range("key '" + last_path + "' not found")); JSON_THROW(out_of_range(403, "key '" + last_path + "' not found"));
} }
} }
else if (parent.is_array()) else if (parent.is_array())
@ -12035,7 +12035,7 @@ class basic_json
// the "path" location must exist - use at() // the "path" location must exist - use at()
success = (result.at(ptr) == get_value("test", "value", false)); success = (result.at(ptr) == get_value("test", "value", false));
} }
JSON_CATCH (std::out_of_range&) JSON_CATCH (out_of_range&)
{ {
// ignore out of range errors: success remains false // ignore out of range errors: success remains false
} }

View file

@ -63,10 +63,12 @@ TEST_CASE("element access 2")
SECTION("access outside bounds") SECTION("access outside bounds")
{ {
CHECK_THROWS_AS(j.at("foo"), std::out_of_range); CHECK_THROWS_AS(j.at("foo"), json::out_of_range);
CHECK_THROWS_AS(j_const.at("foo"), std::out_of_range); CHECK_THROWS_AS(j_const.at("foo"), json::out_of_range);
CHECK_THROWS_WITH(j.at("foo"), "key 'foo' not found"); CHECK_THROWS_WITH(j.at("foo"),
CHECK_THROWS_WITH(j_const.at("foo"), "key 'foo' not found"); "[json.exception.out_of_range.403] key 'foo' not found");
CHECK_THROWS_WITH(j_const.at("foo"),
"[json.exception.out_of_range.403] key 'foo' not found");
} }
SECTION("access on non-object type") SECTION("access on non-object type")

View file

@ -75,8 +75,9 @@ TEST_CASE("JSON patch")
json doc2 = R"({ "q": { "bar": 2 } })"_json; json doc2 = R"({ "q": { "bar": 2 } })"_json;
// because "a" does not exist. // because "a" does not exist.
CHECK_THROWS_AS(doc2.patch(patch), std::out_of_range); CHECK_THROWS_AS(doc2.patch(patch), json::out_of_range);
CHECK_THROWS_WITH(doc2.patch(patch), "key 'a' not found"); CHECK_THROWS_WITH(doc2.patch(patch),
"[json.exception.out_of_range.403] key 'a' not found");
} }
SECTION("4.2 remove") SECTION("4.2 remove")
@ -420,8 +421,9 @@ TEST_CASE("JSON patch")
// references neither the root of the document, nor a member of // references neither the root of the document, nor a member of
// an existing object, nor a member of an existing array. // an existing object, nor a member of an existing array.
CHECK_THROWS_AS(doc.patch(patch), std::out_of_range); CHECK_THROWS_AS(doc.patch(patch), json::out_of_range);
CHECK_THROWS_WITH(doc.patch(patch), "key 'baz' not found"); CHECK_THROWS_WITH(doc.patch(patch),
"[json.exception.out_of_range.403] key 'baz' not found");
} }
// A.13. Invalid JSON Patch Document // A.13. Invalid JSON Patch Document
@ -780,8 +782,9 @@ TEST_CASE("JSON patch")
{ {
json j = {{"foo", 1}, {"bar", 2}}; json j = {{"foo", 1}, {"bar", 2}};
json patch = {{{"op", "remove"}, {"path", "/baz"}}}; json patch = {{{"op", "remove"}, {"path", "/baz"}}};
CHECK_THROWS_AS(j.patch(patch), std::out_of_range); CHECK_THROWS_AS(j.patch(patch), json::out_of_range);
CHECK_THROWS_WITH(j.patch(patch), "key 'baz' not found"); CHECK_THROWS_WITH(j.patch(patch),
"[json.exception.out_of_range.403] key 'baz' not found");
} }
SECTION("root element as target location") SECTION("root element as target location")
@ -835,8 +838,9 @@ TEST_CASE("JSON patch")
{ {
json j = {{"foo", 1}, {"bar", 2}}; json j = {{"foo", 1}, {"bar", 2}};
json patch = {{{"op", "replace"}, {"path", "/baz"}, {"value", 3}}}; json patch = {{{"op", "replace"}, {"path", "/baz"}, {"value", 3}}};
CHECK_THROWS_AS(j.patch(patch), std::out_of_range); CHECK_THROWS_AS(j.patch(patch), json::out_of_range);
CHECK_THROWS_WITH(j.patch(patch), "key 'baz' not found"); CHECK_THROWS_WITH(j.patch(patch),
"[json.exception.out_of_range.403] key 'baz' not found");
} }
} }
@ -891,8 +895,9 @@ TEST_CASE("JSON patch")
{ {
json j = {{"foo", 1}, {"bar", 2}}; json j = {{"foo", 1}, {"bar", 2}};
json patch = {{{"op", "move"}, {"path", "/baz"}, {"from", "/baz"}}}; json patch = {{{"op", "move"}, {"path", "/baz"}, {"from", "/baz"}}};
CHECK_THROWS_AS(j.patch(patch), std::out_of_range); CHECK_THROWS_AS(j.patch(patch), json::out_of_range);
CHECK_THROWS_WITH(j.patch(patch), "key 'baz' not found"); CHECK_THROWS_WITH(j.patch(patch),
"[json.exception.out_of_range.403] key 'baz' not found");
} }
} }
@ -947,8 +952,9 @@ TEST_CASE("JSON patch")
{ {
json j = {{"foo", 1}, {"bar", 2}}; json j = {{"foo", 1}, {"bar", 2}};
json patch = {{{"op", "copy"}, {"path", "/fob"}, {"from", "/baz"}}}; json patch = {{{"op", "copy"}, {"path", "/fob"}, {"from", "/baz"}}};
CHECK_THROWS_AS(j.patch(patch), std::out_of_range); CHECK_THROWS_AS(j.patch(patch), json::out_of_range);
CHECK_THROWS_WITH(j.patch(patch), "key 'baz' not found"); CHECK_THROWS_WITH(j.patch(patch),
"[json.exception.out_of_range.403] key 'baz' not found");
} }
} }

View file

@ -185,8 +185,9 @@ TEST_CASE("JSON pointers")
CHECK(j[json::json_pointer("/m~0n")] == j["m~n"]); CHECK(j[json::json_pointer("/m~0n")] == j["m~n"]);
// unescaped access // unescaped access
CHECK_THROWS_AS(j.at(json::json_pointer("/a/b")), std::out_of_range); CHECK_THROWS_AS(j.at(json::json_pointer("/a/b")), json::out_of_range);
CHECK_THROWS_WITH(j.at(json::json_pointer("/a/b")), "key 'a' not found"); CHECK_THROWS_WITH(j.at(json::json_pointer("/a/b")),
"[json.exception.out_of_range.403] key 'a' not found");
// unresolved access // unresolved access
const json j_primitive = 1; const json j_primitive = 1;