Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
J
json
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Analytics
Analytics
CI / CD
Repository
Value Stream
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Libraries
json
Commits
9374eaa0
Unverified
Commit
9374eaa0
authored
Mar 08, 2017
by
Niels Lohmann
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
🔨
added user-defined exception 501
parent
c8a6ce79
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
56 additions
and
28 deletions
+56
-28
src/json.hpp
src/json.hpp
+25
-13
src/json.hpp.re2c
src/json.hpp.re2c
+25
-9
test/src/unit-json_patch.cpp
test/src/unit-json_patch.cpp
+6
-6
No files found.
src/json.hpp
View file @
9374eaa0
...
...
@@ -52,7 +52,6 @@ SOFTWARE.
#include <memory> // addressof, allocator, allocator_traits, unique_ptr
#include <numeric> // accumulate
#include <sstream> // stringstream
#include <stdexcept> // domain_error, invalid_argument, out_of_range
#include <string> // getline, stoi, string, to_string
#include <type_traits> // add_pointer, conditional, decay, enable_if, false_type, integral_constant, is_arithmetic, is_base_of, is_const, is_constructible, is_convertible, is_default_constructible, is_enum, is_floating_point, is_integral, is_nothrow_move_assignable, is_nothrow_move_constructible, is_pointer, is_reference, is_same, is_scalar, is_signed, remove_const, remove_cv, remove_pointer, remove_reference, true_type, underlying_type
#include <utility> // declval, forward, make_pair, move, pair, swap
...
...
@@ -295,6 +294,26 @@ class out_of_range : public exception
{}
};
/*!
@brief exception indicating other errors
Exceptions have ids 5xx.
name / id | example massage | description
------------------------------ | --------------- | -------------------------
json.exception.[other_error](@ref other_error).501 | "unsuccessful" | A JSON Patch operation 'test' failed.
@since version 3.0.0
*/
class
other_error
:
public
exception
{
public:
other_error
(
int
id_
,
const
std
::
string
&
what_arg_
)
:
exception
(
id_
,
"other_error"
,
what_arg_
)
{}
};
///////////////////////////
// JSON type enumeration //
...
...
@@ -1240,12 +1259,6 @@ class basic_json
/// Classes to implement user-defined exceptions.
/// @{
/*
name / id | example massage | description
------------------------------ | --------------- | -------------------------
json.exception.other.500 | "unsuccessful" | A JSON Patch operation 'test' failed.
*/
/// @copydoc detail::parse_error
using
parse_error
=
detail
::
parse_error
;
/// @copydoc detail::invalid_iterator
...
...
@@ -1254,6 +1267,8 @@ class basic_json
using
type_error
=
detail
::
type_error
;
/// @copydoc detail::out_of_range
using
out_of_range
=
detail
::
out_of_range
;
/// @copydoc detail::other_error
using
other_error
=
detail
::
other_error
;
/// @}
...
...
@@ -1948,7 +1963,7 @@ class basic_json
{
if
(
t
==
value_t
::
null
)
{
JSON_THROW
(
std
::
domain_error
(
"961c151d2e87f2686a955a9be24d316f1362bf21 2.1.1"
));
// LCOV_EXCL_LINE
JSON_THROW
(
other_error
(
500
,
"961c151d2e87f2686a955a9be24d316f1362bf21 2.1.1"
));
// LCOV_EXCL_LINE
}
break
;
}
...
...
@@ -7374,11 +7389,8 @@ class basic_json
template
<
typename
T
>
static
T
get_from_vector
(
const
std
::
vector
<
uint8_t
>&
vec
,
const
size_t
current_index
)
{
// check if we can read sizeof(T) bytes starting the next index
check_length
(
vec
.
size
(),
sizeof
(
T
),
current_index
+
1
);
//if (current_index + sizeof(T) + 1 > vec.size())
//{
// JSON_THROW(parse_error(110, current_index + 1, "cannot read " + std::to_string(sizeof(T)) + " bytes from vector"));
//}
T
result
;
auto
*
ptr
=
reinterpret_cast
<
uint8_t
*>
(
&
result
);
...
...
@@ -13062,7 +13074,7 @@ basic_json_parser_74:
// throw an exception if test fails
if
(
not
success
)
{
JSON_THROW
(
std
::
domain_error
(
"unsuccessful: "
+
val
.
dump
()));
JSON_THROW
(
other_error
(
501
,
"unsuccessful: "
+
val
.
dump
()));
}
break
;
...
...
src/json.hpp.re2c
View file @
9374eaa0
...
...
@@ -52,7 +52,6 @@ SOFTWARE.
#include <memory> // addressof, allocator, allocator_traits, unique_ptr
#include <numeric> // accumulate
#include <sstream> // stringstream
#include <stdexcept> // domain_error, invalid_argument, out_of_range
#include <string> // getline, stoi, string, to_string
#include <type_traits> // add_pointer, conditional, decay, enable_if, false_type, integral_constant, is_arithmetic, is_base_of, is_const, is_constructible, is_convertible, is_default_constructible, is_enum, is_floating_point, is_integral, is_nothrow_move_assignable, is_nothrow_move_constructible, is_pointer, is_reference, is_same, is_scalar, is_signed, remove_const, remove_cv, remove_pointer, remove_reference, true_type, underlying_type
#include <utility> // declval, forward, make_pair, move, pair, swap
...
...
@@ -295,6 +294,27 @@ class out_of_range : public exception
{}
};
/*!
@brief exception indicating other errors
Exceptions have ids 5xx.
name / id | example massage | description
------------------------------ | --------------- | -------------------------
json.exception.[other_error](@ref other_error).501 | "unsuccessful: {"op":"test","path":"/baz",
"value":"bar"}" | A JSON Patch operation 'test' failed.
@since version 3.0.0
*/
class other_error : public exception
{
public:
other_error(int id_, const std::string& what_arg_)
: exception(id_, "other_error", what_arg_)
{}
};
///////////////////////////
// JSON type enumeration //
...
...
@@ -1240,12 +1260,6 @@ class basic_json
/// Classes to implement user-defined exceptions.
/// @{
/*
name / id | example massage | description
------------------------------ | --------------- | -------------------------
json.exception.other.500 | "unsuccessful" | A JSON Patch operation 'test' failed.
*/
/// @copydoc detail::parse_error
using parse_error = detail::parse_error;
/// @copydoc detail::invalid_iterator
...
...
@@ -1254,6 +1268,8 @@ class basic_json
using type_error = detail::type_error;
/// @copydoc detail::out_of_range
using out_of_range = detail::out_of_range;
/// @copydoc detail::other_error
using other_error = detail::other_error;
/// @}
...
...
@@ -1948,7 +1964,7 @@ class basic_json
{
if (t == value_t::null)
{
JSON_THROW(
std::domain_error(
"961c151d2e87f2686a955a9be24d316f1362bf21 2.1.1")); // LCOV_EXCL_LINE
JSON_THROW(
other_error(500,
"961c151d2e87f2686a955a9be24d316f1362bf21 2.1.1")); // LCOV_EXCL_LINE
}
break;
}
...
...
@@ -12092,7 +12108,7 @@ class basic_json
// throw an exception if test fails
if (not success)
{
JSON_THROW(
std::domain_error(
"unsuccessful: " + val.dump()));
JSON_THROW(
other_error(501,
"unsuccessful: " + val.dump()));
}
break;
...
...
test/src/unit-json_patch.cpp
View file @
9374eaa0
...
...
@@ -337,8 +337,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
::
other
_error
);
CHECK_THROWS_WITH
(
doc
.
patch
(
patch
),
"
[json.exception.other_error.501]
unsuccessful: "
+
patch
[
0
].
dump
());
}
SECTION
(
"A.10. Adding a Nested Member Object"
)
...
...
@@ -478,8 +478,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
::
other
_error
);
CHECK_THROWS_WITH
(
doc
.
patch
(
patch
),
"
[json.exception.other_error.501]
unsuccessful: "
+
patch
[
0
].
dump
());
}
SECTION
(
"A.16. Adding an Array Value"
)
...
...
@@ -1177,8 +1177,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
::
other
_error
);
CHECK_THROWS_WITH
(
doc
.
patch
(
patch
),
"
[json.exception.other_error.501]
unsuccessful: "
+
patch
[
0
].
dump
());
}
}
}
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment