Skip to content

Commit 7df5094

Browse files
authored
Merge pull request #688 from evoskuil/master
Add boost json error conversions.
2 parents 1f6dc0d + c8bb87f commit 7df5094

File tree

7 files changed

+611
-146
lines changed

7 files changed

+611
-146
lines changed

include/bitcoin/network/error.hpp

Lines changed: 63 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,9 @@ namespace error {
4141
/// These are platform-specific error codes, for which we have seen variance.
4242
/// boost::system::errc::errc_t is the boost::system error condition enum.
4343
/// By comparing against conditions we obtain platform-independent error codes.
44+
typedef boost::json::error json_error_t;
4445
typedef boost::beast::http::error http_error_t;
46+
typedef boost::beast::websocket::error ws_error_t;
4547
typedef boost::system::errc::errc_t boost_error_t;
4648
typedef boost::asio::error::misc_errors asio_misc_error_t;
4749
typedef boost::asio::error::netdb_errors asio_netdb_error_t;
@@ -229,6 +231,47 @@ enum error_t : uint8_t
229231
bad_close_size,
230232
bad_close_payload,
231233

234+
// boost json error
235+
syntax,
236+
extra_data,
237+
incomplete,
238+
exponent_overflow,
239+
too_deep,
240+
illegal_leading_surrogate,
241+
illegal_trailing_surrogate,
242+
expected_hex_digit,
243+
expected_utf16_escape,
244+
object_too_large,
245+
array_too_large,
246+
key_too_large,
247+
string_too_large,
248+
number_too_large,
249+
input_error,
250+
exception,
251+
out_of_range,
252+
test_failure,
253+
missing_slash,
254+
invalid_escape,
255+
token_not_number,
256+
value_is_scalar,
257+
json_not_found,
258+
token_overflow,
259+
past_the_end,
260+
not_number,
261+
not_exact,
262+
not_null,
263+
not_bool,
264+
not_array,
265+
not_object,
266+
not_string,
267+
not_int64,
268+
not_uint64,
269+
not_double,
270+
not_integer,
271+
size_mismatch,
272+
exhausted_variants,
273+
unknown_name,
274+
232275
// rpc error
233276
message_overflow,
234277
undefined_type,
@@ -244,21 +287,25 @@ enum error_t : uint8_t
244287
// No current need for error_code equivalence mapping.
245288
DECLARE_ERROR_T_CODE_CATEGORY(error);
246289

247-
/// Construct a boost_code object from a boost system errc_t.
248-
/// Unfortunately boost_code does not have this std::error_code construction.
249-
inline boost_code to_boost_code(boost_error_t ec) NOEXCEPT
290+
inline boost_code to_system_code(boost_error_t ec) NOEXCEPT
250291
{
251-
return boost_code{ ec, boost::system::generic_category() };
292+
return boost::system::errc::make_error_code(ec);
252293
}
253294

254-
/// Unfortunately boost_code does not have this std::error_code construction.
255295
inline boost_code to_http_code(http_error_t ec) NOEXCEPT
256296
{
257297
return boost::beast::http::make_error_code(ec);
258298
}
259299

260-
/// Shortcircuit common boost code mapping.
261-
BCT_API bool asio_is_canceled(const boost_code& ec) NOEXCEPT;
300+
inline boost_code to_ws_code(ws_error_t ec) NOEXCEPT
301+
{
302+
return boost::beast::websocket::make_error_code(ec);
303+
}
304+
305+
inline boost_code to_json_code(json_error_t ec) NOEXCEPT
306+
{
307+
return boost::json::make_error_code(ec);
308+
}
262309

263310
/// Unfortunately std::error_code and boost::system::error_code are distinct
264311
/// types, so they do not compare as would be expected across distinct
@@ -269,14 +316,22 @@ BCT_API bool asio_is_canceled(const boost_code& ec) NOEXCEPT;
269316
/// codes to network::error_t codes. This cannot be done using the equivalence
270317
/// operator overloads of either, since the error_code types are distinct,
271318
/// despite being effectively identical. So we provide this explicit mapping.
319+
320+
/// Shortcircuit common boost code mapping.
321+
BCT_API bool asio_is_canceled(const boost_code& ec) NOEXCEPT;
322+
323+
/// mapping of boost::asio error codes to network (or error::unknown).
272324
BCT_API code asio_to_error_code(const boost_code& ec) NOEXCEPT;
273325

274326
/// 1:1 mapping of boost::beast:http::error to network (or error::unknown).
275327
BCT_API code http_to_error_code(const boost_code& ec) NOEXCEPT;
276328

277-
/// 1:1 mapping of boost::beast:websocket::error to network (or error::unknown).
329+
/// 1:1 mapping of boost::beast::websocket::error to network (or error::unknown).
278330
BCT_API code ws_to_error_code(const boost_code& ec) NOEXCEPT;
279331

332+
/// 1:1 mapping of boost::json::error to network (or error::unknown).
333+
BCT_API code json_to_error_code(const boost_code& ec) NOEXCEPT;
334+
280335
} // namespace error
281336
} // namespace network
282337
} // namespace libbitcoin

include/bitcoin/network/impl/messages/json_body.ipp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ void CLASS::reader::init(const http::length_type& length,
3535
boost_code& ec) NOEXCEPT
3636
{
3737
BC_PUSH_WARNING(NO_THROW_IN_NOEXCEPT)
38-
const auto value = length.get_value_or(zero);
38+
const auto value = length.get_value_or(max_size_t);
3939
BC_POP_WARNING()
4040

4141
using namespace system;

0 commit comments

Comments
 (0)