🔨 added user-defined exceptions 307

This commit is contained in:
Niels Lohmann 2017-03-05 19:26:44 +01:00
parent bb740c43fb
commit 2a9393af00
No known key found for this signature in database
GPG key ID: 7F3CEA63AE251B69
4 changed files with 91 additions and 64 deletions

View file

@ -4397,8 +4397,8 @@ class basic_json
@post Invalidates iterators and references at or after the point of the @post Invalidates iterators and references at or after the point of the
erase, including the `end()` iterator. erase, including the `end()` iterator.
@throw std::domain_error if called on a `null` value; example: `"cannot @throw type_error.307 if called on a `null` value; example: `"cannot use
use erase() with null"` erase() with null"`
@throw invalid_iterator.202 if called on an iterator which does not belong @throw invalid_iterator.202 if called on an iterator which does not belong
to the current JSON value; example: `"iterator does not fit current to the current JSON value; example: `"iterator does not fit current
value"` value"`
@ -4478,7 +4478,7 @@ class basic_json
default: default:
{ {
JSON_THROW(std::domain_error("cannot use erase() with " + type_name())); JSON_THROW(type_error(307, "cannot use erase() with " + type_name()));
} }
} }
@ -4505,8 +4505,8 @@ class basic_json
@post Invalidates iterators and references at or after the point of the @post Invalidates iterators and references at or after the point of the
erase, including the `end()` iterator. erase, including the `end()` iterator.
@throw std::domain_error if called on a `null` value; example: `"cannot @throw type_error.307 if called on a `null` value; example: `"cannot use
use erase() with null"` erase() with null"`
@throw invalid_iterator.203 if called on iterators which does not belong @throw invalid_iterator.203 if called on iterators which does not belong
to the current JSON value; example: `"iterators do not fit current value"` to the current JSON value; example: `"iterators do not fit current value"`
@throw invalid_iterator.204 if called on a primitive type with invalid @throw invalid_iterator.204 if called on a primitive type with invalid
@ -4587,7 +4587,7 @@ class basic_json
default: default:
{ {
JSON_THROW(std::domain_error("cannot use erase() with " + type_name())); JSON_THROW(type_error(307, "cannot use erase() with " + type_name()));
} }
} }
@ -4608,7 +4608,7 @@ class basic_json
@post References and iterators to the erased elements are invalidated. @post References and iterators to the erased elements are invalidated.
Other references and iterators are not affected. Other references and iterators are not affected.
@throw std::domain_error when called on a type other than JSON object; @throw type_error.307 when called on a type other than JSON object;
example: `"cannot use erase() with null"` example: `"cannot use erase() with null"`
@complexity `log(size()) + count(key)` @complexity `log(size()) + count(key)`
@ -4631,7 +4631,7 @@ class basic_json
return m_value.object->erase(key); return m_value.object->erase(key);
} }
JSON_THROW(std::domain_error("cannot use erase() with " + type_name())); JSON_THROW(type_error(307, "cannot use erase() with " + type_name()));
} }
/*! /*!
@ -4641,7 +4641,7 @@ class basic_json
@param[in] idx index of the element to remove @param[in] idx index of the element to remove
@throw std::domain_error when called on a type other than JSON array; @throw type_error.307 when called on a type other than JSON object;
example: `"cannot use erase() with null"` example: `"cannot use erase() with null"`
@throw std::out_of_range when `idx >= size()`; example: `"array index 17 @throw std::out_of_range when `idx >= size()`; example: `"array index 17
is out of range"` is out of range"`
@ -4672,7 +4672,7 @@ class basic_json
} }
else else
{ {
JSON_THROW(std::domain_error("cannot use erase() with " + type_name())); JSON_THROW(type_error(307, "cannot use erase() with " + type_name()));
} }
} }

View file

@ -4397,8 +4397,8 @@ class basic_json
@post Invalidates iterators and references at or after the point of the @post Invalidates iterators and references at or after the point of the
erase, including the `end()` iterator. erase, including the `end()` iterator.
@throw std::domain_error if called on a `null` value; example: `"cannot @throw type_error.307 if called on a `null` value; example: `"cannot use
use erase() with null"` erase() with null"`
@throw invalid_iterator.202 if called on an iterator which does not belong @throw invalid_iterator.202 if called on an iterator which does not belong
to the current JSON value; example: `"iterator does not fit current to the current JSON value; example: `"iterator does not fit current
value"` value"`
@ -4478,7 +4478,7 @@ class basic_json
default: default:
{ {
JSON_THROW(std::domain_error("cannot use erase() with " + type_name())); JSON_THROW(type_error(307, "cannot use erase() with " + type_name()));
} }
} }
@ -4505,8 +4505,8 @@ class basic_json
@post Invalidates iterators and references at or after the point of the @post Invalidates iterators and references at or after the point of the
erase, including the `end()` iterator. erase, including the `end()` iterator.
@throw std::domain_error if called on a `null` value; example: `"cannot @throw type_error.307 if called on a `null` value; example: `"cannot use
use erase() with null"` erase() with null"`
@throw invalid_iterator.203 if called on iterators which does not belong @throw invalid_iterator.203 if called on iterators which does not belong
to the current JSON value; example: `"iterators do not fit current value"` to the current JSON value; example: `"iterators do not fit current value"`
@throw invalid_iterator.204 if called on a primitive type with invalid @throw invalid_iterator.204 if called on a primitive type with invalid
@ -4587,7 +4587,7 @@ class basic_json
default: default:
{ {
JSON_THROW(std::domain_error("cannot use erase() with " + type_name())); JSON_THROW(type_error(307, "cannot use erase() with " + type_name()));
} }
} }
@ -4608,7 +4608,7 @@ class basic_json
@post References and iterators to the erased elements are invalidated. @post References and iterators to the erased elements are invalidated.
Other references and iterators are not affected. Other references and iterators are not affected.
@throw std::domain_error when called on a type other than JSON object; @throw type_error.307 when called on a type other than JSON object;
example: `"cannot use erase() with null"` example: `"cannot use erase() with null"`
@complexity `log(size()) + count(key)` @complexity `log(size()) + count(key)`
@ -4631,7 +4631,7 @@ class basic_json
return m_value.object->erase(key); return m_value.object->erase(key);
} }
JSON_THROW(std::domain_error("cannot use erase() with " + type_name())); JSON_THROW(type_error(307, "cannot use erase() with " + type_name()));
} }
/*! /*!
@ -4641,7 +4641,7 @@ class basic_json
@param[in] idx index of the element to remove @param[in] idx index of the element to remove
@throw std::domain_error when called on a type other than JSON array; @throw type_error.307 when called on a type other than JSON object;
example: `"cannot use erase() with null"` example: `"cannot use erase() with null"`
@throw std::out_of_range when `idx >= size()`; example: `"array index 17 @throw std::out_of_range when `idx >= size()`; example: `"array index 17
is out of range"` is out of range"`
@ -4672,7 +4672,7 @@ class basic_json
} }
else else
{ {
JSON_THROW(std::domain_error("cannot use erase() with " + type_name())); JSON_THROW(type_error(307, "cannot use erase() with " + type_name()));
} }
} }

View file

@ -444,50 +444,57 @@ TEST_CASE("element access 1")
SECTION("null") SECTION("null")
{ {
json j_nonobject(json::value_t::null); json j_nonobject(json::value_t::null);
CHECK_THROWS_AS(j_nonobject.erase(0), std::domain_error); CHECK_THROWS_AS(j_nonobject.erase(0), json::type_error);
CHECK_THROWS_WITH(j_nonobject.erase(0), "cannot use erase() with null"); CHECK_THROWS_WITH(j_nonobject.erase(0),
"[json.exception.type_error.307] cannot use erase() with null");
} }
SECTION("boolean") SECTION("boolean")
{ {
json j_nonobject(json::value_t::boolean); json j_nonobject(json::value_t::boolean);
CHECK_THROWS_AS(j_nonobject.erase(0), std::domain_error); CHECK_THROWS_AS(j_nonobject.erase(0), json::type_error);
CHECK_THROWS_WITH(j_nonobject.erase(0), "cannot use erase() with boolean"); CHECK_THROWS_WITH(j_nonobject.erase(0),
"[json.exception.type_error.307] cannot use erase() with boolean");
} }
SECTION("string") SECTION("string")
{ {
json j_nonobject(json::value_t::string); json j_nonobject(json::value_t::string);
CHECK_THROWS_AS(j_nonobject.erase(0), std::domain_error); CHECK_THROWS_AS(j_nonobject.erase(0), json::type_error);
CHECK_THROWS_WITH(j_nonobject.erase(0), "cannot use erase() with string"); CHECK_THROWS_WITH(j_nonobject.erase(0),
"[json.exception.type_error.307] cannot use erase() with string");
} }
SECTION("object") SECTION("object")
{ {
json j_nonobject(json::value_t::object); json j_nonobject(json::value_t::object);
CHECK_THROWS_AS(j_nonobject.erase(0), std::domain_error); CHECK_THROWS_AS(j_nonobject.erase(0), json::type_error);
CHECK_THROWS_WITH(j_nonobject.erase(0), "cannot use erase() with object"); CHECK_THROWS_WITH(j_nonobject.erase(0),
"[json.exception.type_error.307] cannot use erase() with object");
} }
SECTION("number (integer)") SECTION("number (integer)")
{ {
json j_nonobject(json::value_t::number_integer); json j_nonobject(json::value_t::number_integer);
CHECK_THROWS_AS(j_nonobject.erase(0), std::domain_error); CHECK_THROWS_AS(j_nonobject.erase(0), json::type_error);
CHECK_THROWS_WITH(j_nonobject.erase(0), "cannot use erase() with number"); CHECK_THROWS_WITH(j_nonobject.erase(0),
"[json.exception.type_error.307] cannot use erase() with number");
} }
SECTION("number (unsigned)") SECTION("number (unsigned)")
{ {
json j_nonobject(json::value_t::number_unsigned); json j_nonobject(json::value_t::number_unsigned);
CHECK_THROWS_AS(j_nonobject.erase(0), std::domain_error); CHECK_THROWS_AS(j_nonobject.erase(0), json::type_error);
CHECK_THROWS_WITH(j_nonobject.erase(0), "cannot use erase() with number"); CHECK_THROWS_WITH(j_nonobject.erase(0),
"[json.exception.type_error.307] cannot use erase() with number");
} }
SECTION("number (floating-point)") SECTION("number (floating-point)")
{ {
json j_nonobject(json::value_t::number_float); json j_nonobject(json::value_t::number_float);
CHECK_THROWS_AS(j_nonobject.erase(0), std::domain_error); CHECK_THROWS_AS(j_nonobject.erase(0), json::type_error);
CHECK_THROWS_WITH(j_nonobject.erase(0), "cannot use erase() with number"); CHECK_THROWS_WITH(j_nonobject.erase(0),
"[json.exception.type_error.307] cannot use erase() with number");
} }
} }
} }
@ -592,13 +599,15 @@ TEST_CASE("element access 1")
{ {
{ {
json j; json j;
CHECK_THROWS_AS(j.erase(j.begin()), std::domain_error); CHECK_THROWS_AS(j.erase(j.begin()), json::type_error);
CHECK_THROWS_WITH(j.erase(j.begin()), "cannot use erase() with null"); CHECK_THROWS_WITH(j.erase(j.begin()),
"[json.exception.type_error.307] cannot use erase() with null");
} }
{ {
json j; json j;
CHECK_THROWS_AS(j.erase(j.cbegin()), std::domain_error); CHECK_THROWS_AS(j.erase(j.cbegin()), json::type_error);
CHECK_THROWS_WITH(j.erase(j.begin()), "cannot use erase() with null"); CHECK_THROWS_WITH(j.erase(j.begin()),
"[json.exception.type_error.307] cannot use erase() with null");
} }
} }
@ -690,12 +699,14 @@ TEST_CASE("element access 1")
{ {
json j = "foo"; json j = "foo";
CHECK_THROWS_AS(j.erase(j.end()), json::invalid_iterator); CHECK_THROWS_AS(j.erase(j.end()), json::invalid_iterator);
CHECK_THROWS_WITH(j.erase(j.end()), "[json.exception.invalid_iterator.205] iterator out of range"); CHECK_THROWS_WITH(j.erase(j.end()),
"[json.exception.invalid_iterator.205] iterator out of range");
} }
{ {
json j = "bar"; json j = "bar";
CHECK_THROWS_AS(j.erase(j.cend()), json::invalid_iterator); CHECK_THROWS_AS(j.erase(j.cend()), json::invalid_iterator);
CHECK_THROWS_WITH(j.erase(j.cend()), "[json.exception.invalid_iterator.205] iterator out of range"); CHECK_THROWS_WITH(j.erase(j.cend()),
"[json.exception.invalid_iterator.205] iterator out of range");
} }
} }
@ -704,12 +715,14 @@ TEST_CASE("element access 1")
{ {
json j = false; json j = false;
CHECK_THROWS_AS(j.erase(j.end()), json::invalid_iterator); CHECK_THROWS_AS(j.erase(j.end()), json::invalid_iterator);
CHECK_THROWS_WITH(j.erase(j.end()), "[json.exception.invalid_iterator.205] iterator out of range"); CHECK_THROWS_WITH(j.erase(j.end()),
"[json.exception.invalid_iterator.205] iterator out of range");
} }
{ {
json j = true; json j = true;
CHECK_THROWS_AS(j.erase(j.cend()), json::invalid_iterator); CHECK_THROWS_AS(j.erase(j.cend()), json::invalid_iterator);
CHECK_THROWS_WITH(j.erase(j.cend()), "[json.exception.invalid_iterator.205] iterator out of range"); CHECK_THROWS_WITH(j.erase(j.cend()),
"[json.exception.invalid_iterator.205] iterator out of range");
} }
} }
@ -718,12 +731,14 @@ TEST_CASE("element access 1")
{ {
json j = 17; json j = 17;
CHECK_THROWS_AS(j.erase(j.end()), json::invalid_iterator); CHECK_THROWS_AS(j.erase(j.end()), json::invalid_iterator);
CHECK_THROWS_WITH(j.erase(j.end()), "[json.exception.invalid_iterator.205] iterator out of range"); CHECK_THROWS_WITH(j.erase(j.end()),
"[json.exception.invalid_iterator.205] iterator out of range");
} }
{ {
json j = 17; json j = 17;
CHECK_THROWS_AS(j.erase(j.cend()), json::invalid_iterator); CHECK_THROWS_AS(j.erase(j.cend()), json::invalid_iterator);
CHECK_THROWS_WITH(j.erase(j.cend()), "[json.exception.invalid_iterator.205] iterator out of range"); CHECK_THROWS_WITH(j.erase(j.cend()),
"[json.exception.invalid_iterator.205] iterator out of range");
} }
} }
@ -732,12 +747,14 @@ TEST_CASE("element access 1")
{ {
json j = 17u; json j = 17u;
CHECK_THROWS_AS(j.erase(j.end()), json::invalid_iterator); CHECK_THROWS_AS(j.erase(j.end()), json::invalid_iterator);
CHECK_THROWS_WITH(j.erase(j.end()), "[json.exception.invalid_iterator.205] iterator out of range"); CHECK_THROWS_WITH(j.erase(j.end()),
"[json.exception.invalid_iterator.205] iterator out of range");
} }
{ {
json j = 17u; json j = 17u;
CHECK_THROWS_AS(j.erase(j.cend()), json::invalid_iterator); CHECK_THROWS_AS(j.erase(j.cend()), json::invalid_iterator);
CHECK_THROWS_WITH(j.erase(j.cend()), "[json.exception.invalid_iterator.205] iterator out of range"); CHECK_THROWS_WITH(j.erase(j.cend()),
"[json.exception.invalid_iterator.205] iterator out of range");
} }
} }
@ -746,12 +763,14 @@ TEST_CASE("element access 1")
{ {
json j = 23.42; json j = 23.42;
CHECK_THROWS_AS(j.erase(j.end()), json::invalid_iterator); CHECK_THROWS_AS(j.erase(j.end()), json::invalid_iterator);
CHECK_THROWS_WITH(j.erase(j.end()), "[json.exception.invalid_iterator.205] iterator out of range"); CHECK_THROWS_WITH(j.erase(j.end()),
"[json.exception.invalid_iterator.205] iterator out of range");
} }
{ {
json j = 23.42; json j = 23.42;
CHECK_THROWS_AS(j.erase(j.cend()), json::invalid_iterator); CHECK_THROWS_AS(j.erase(j.cend()), json::invalid_iterator);
CHECK_THROWS_WITH(j.erase(j.cend()), "[json.exception.invalid_iterator.205] iterator out of range"); CHECK_THROWS_WITH(j.erase(j.cend()),
"[json.exception.invalid_iterator.205] iterator out of range");
} }
} }
} }
@ -762,13 +781,15 @@ TEST_CASE("element access 1")
{ {
{ {
json j; json j;
CHECK_THROWS_AS(j.erase(j.begin(), j.end()), std::domain_error); CHECK_THROWS_AS(j.erase(j.begin(), j.end()), json::type_error);
CHECK_THROWS_WITH(j.erase(j.begin(), j.end()), "cannot use erase() with null"); CHECK_THROWS_WITH(j.erase(j.begin(), j.end()),
"[json.exception.type_error.307] cannot use erase() with null");
} }
{ {
json j; json j;
CHECK_THROWS_AS(j.erase(j.cbegin(), j.cend()), std::domain_error); CHECK_THROWS_AS(j.erase(j.cbegin(), j.cend()), json::type_error);
CHECK_THROWS_WITH(j.erase(j.cbegin(), j.cend()), "cannot use erase() with null"); CHECK_THROWS_WITH(j.erase(j.cbegin(), j.cend()),
"[json.exception.type_error.307] cannot use erase() with null");
} }
} }

View file

@ -757,43 +757,49 @@ TEST_CASE("element access 2")
SECTION("null") SECTION("null")
{ {
json j_nonobject(json::value_t::null); json j_nonobject(json::value_t::null);
CHECK_THROWS_AS(j_nonobject.erase("foo"), std::domain_error); CHECK_THROWS_AS(j_nonobject.erase("foo"), json::type_error);
CHECK_THROWS_WITH(j_nonobject.erase("foo"), "cannot use erase() with null"); CHECK_THROWS_WITH(j_nonobject.erase("foo"),
"[json.exception.type_error.307] cannot use erase() with null");
} }
SECTION("boolean") SECTION("boolean")
{ {
json j_nonobject(json::value_t::boolean); json j_nonobject(json::value_t::boolean);
CHECK_THROWS_AS(j_nonobject.erase("foo"), std::domain_error); CHECK_THROWS_AS(j_nonobject.erase("foo"), json::type_error);
CHECK_THROWS_WITH(j_nonobject.erase("foo"), "cannot use erase() with boolean"); CHECK_THROWS_WITH(j_nonobject.erase("foo"),
"[json.exception.type_error.307] cannot use erase() with boolean");
} }
SECTION("string") SECTION("string")
{ {
json j_nonobject(json::value_t::string); json j_nonobject(json::value_t::string);
CHECK_THROWS_AS(j_nonobject.erase("foo"), std::domain_error); CHECK_THROWS_AS(j_nonobject.erase("foo"), json::type_error);
CHECK_THROWS_WITH(j_nonobject.erase("foo"), "cannot use erase() with string"); CHECK_THROWS_WITH(j_nonobject.erase("foo"),
"[json.exception.type_error.307] cannot use erase() with string");
} }
SECTION("array") SECTION("array")
{ {
json j_nonobject(json::value_t::array); json j_nonobject(json::value_t::array);
CHECK_THROWS_AS(j_nonobject.erase("foo"), std::domain_error); CHECK_THROWS_AS(j_nonobject.erase("foo"), json::type_error);
CHECK_THROWS_WITH(j_nonobject.erase("foo"), "cannot use erase() with array"); CHECK_THROWS_WITH(j_nonobject.erase("foo"),
"[json.exception.type_error.307] cannot use erase() with array");
} }
SECTION("number (integer)") SECTION("number (integer)")
{ {
json j_nonobject(json::value_t::number_integer); json j_nonobject(json::value_t::number_integer);
CHECK_THROWS_AS(j_nonobject.erase("foo"), std::domain_error); CHECK_THROWS_AS(j_nonobject.erase("foo"), json::type_error);
CHECK_THROWS_WITH(j_nonobject.erase("foo"), "cannot use erase() with number"); CHECK_THROWS_WITH(j_nonobject.erase("foo"),
"[json.exception.type_error.307] cannot use erase() with number");
} }
SECTION("number (floating-point)") SECTION("number (floating-point)")
{ {
json j_nonobject(json::value_t::number_float); json j_nonobject(json::value_t::number_float);
CHECK_THROWS_AS(j_nonobject.erase("foo"), std::domain_error); CHECK_THROWS_AS(j_nonobject.erase("foo"), json::type_error);
CHECK_THROWS_WITH(j_nonobject.erase("foo"), "cannot use erase() with number"); CHECK_THROWS_WITH(j_nonobject.erase("foo"),
"[json.exception.type_error.307] cannot use erase() with number");
} }
} }
} }