error.ipp 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  1. //
  2. // Copyright (c) 2019 Vinnie Falco ([email protected])
  3. //
  4. // Distributed under the Boost Software License, Version 1.0. (See accompanying
  5. // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
  6. //
  7. // Official repository: https://github.com/boostorg/json
  8. //
  9. #ifndef BOOST_JSON_IMPL_ERROR_IPP
  10. #define BOOST_JSON_IMPL_ERROR_IPP
  11. #include <boost/json/error.hpp>
  12. namespace boost {
  13. namespace json {
  14. namespace detail {
  15. // msvc 14.0 has a bug that warns about inability to use constexpr
  16. // construction here, even though there's no constexpr construction
  17. #if defined(_MSC_VER) && _MSC_VER <= 1900
  18. # pragma warning( push )
  19. # pragma warning( disable : 4592 )
  20. #endif
  21. BOOST_JSON_CONSTINIT
  22. error_code_category_t error_code_category;
  23. BOOST_JSON_CONSTINIT
  24. error_condition_category_t error_condition_category;
  25. #if defined(_MSC_VER) && _MSC_VER <= 1900
  26. # pragma warning( pop )
  27. #endif
  28. char const*
  29. error_code_category_t::name() const noexcept
  30. {
  31. return "boost.json";
  32. }
  33. char const*
  34. error_code_category_t::message( int ev, char*, std::size_t ) const noexcept
  35. {
  36. switch(static_cast<error>(ev))
  37. {
  38. default:
  39. case error::syntax: return "syntax error";
  40. case error::extra_data: return "extra data";
  41. case error::incomplete: return "incomplete JSON";
  42. case error::exponent_overflow: return "exponent overflow";
  43. case error::too_deep: return "too deep";
  44. case error::illegal_leading_surrogate: return "illegal leading surrogate";
  45. case error::illegal_trailing_surrogate: return "illegal trailing surrogate";
  46. case error::expected_hex_digit: return "expected hex digit";
  47. case error::expected_utf16_escape: return "expected utf16 escape";
  48. case error::object_too_large: return "object too large";
  49. case error::array_too_large: return "array too large";
  50. case error::key_too_large: return "key too large";
  51. case error::string_too_large: return "string too large";
  52. case error::number_too_large: return "number too large";
  53. case error::input_error: return "input error";
  54. case error::exception: return "got exception";
  55. case error::out_of_range: return "out of range";
  56. case error::test_failure: return "test failure";
  57. case error::missing_slash: return "missing slash character";
  58. case error::invalid_escape: return "invalid escape sequence";
  59. case error::token_not_number: return "token is not a number";
  60. case error::value_is_scalar: return "current value is scalar";
  61. case error::not_found: return "no referenced value";
  62. case error::token_overflow: return "token overflow";
  63. case error::past_the_end: return "past-the-end token not supported";
  64. case error::not_number: return "not a number";
  65. case error::not_exact: return "not exact";
  66. case error::not_null: return "value is not null";
  67. case error::not_bool: return "value is not boolean";
  68. case error::not_array: return "value is not an array";
  69. case error::not_object: return "value is not an object";
  70. case error::not_string: return "value is not a string";
  71. case error::not_int64: return "value is not a std::int64_t number";
  72. case error::not_uint64: return "value is not a std::uint64_t number";
  73. case error::not_double: return "value is not a double";
  74. case error::not_integer: return "value is not integer";
  75. case error::size_mismatch: return "source composite size does not match target size";
  76. case error::exhausted_variants: return "exhausted all variants";
  77. case error::unknown_name: return "unknown name";
  78. }
  79. }
  80. std::string
  81. error_code_category_t::message( int ev ) const
  82. {
  83. return message( ev, nullptr, 0 );
  84. }
  85. system::error_condition
  86. error_code_category_t::default_error_condition( int ev) const noexcept
  87. {
  88. switch(static_cast<error>(ev))
  89. {
  90. default:
  91. return {ev, *this};
  92. case error::syntax:
  93. case error::extra_data:
  94. case error::incomplete:
  95. case error::exponent_overflow:
  96. case error::too_deep:
  97. case error::illegal_leading_surrogate:
  98. case error::illegal_trailing_surrogate:
  99. case error::expected_hex_digit:
  100. case error::expected_utf16_escape:
  101. case error::object_too_large:
  102. case error::array_too_large:
  103. case error::key_too_large:
  104. case error::string_too_large:
  105. case error::number_too_large:
  106. case error::input_error:
  107. return condition::parse_error;
  108. case error::missing_slash:
  109. case error::invalid_escape:
  110. return condition::pointer_parse_error;
  111. case error::token_not_number:
  112. case error::value_is_scalar:
  113. case error::not_found:
  114. case error::token_overflow:
  115. case error::past_the_end:
  116. return condition::pointer_use_error;
  117. case error::not_number:
  118. case error::not_exact:
  119. case error::not_null:
  120. case error::not_bool:
  121. case error::not_array:
  122. case error::not_object:
  123. case error::not_string:
  124. case error::not_int64:
  125. case error::not_uint64:
  126. case error::not_double:
  127. case error::not_integer:
  128. case error::size_mismatch:
  129. case error::exhausted_variants:
  130. case error::unknown_name:
  131. return condition::conversion_error;
  132. case error::exception:
  133. case error::out_of_range:
  134. return condition::generic_error;
  135. }
  136. }
  137. char const*
  138. error_condition_category_t::name() const noexcept
  139. {
  140. return "boost.json";
  141. }
  142. char const*
  143. error_condition_category_t::message( int cv, char*, std::size_t ) const noexcept
  144. {
  145. switch(static_cast<condition>(cv))
  146. {
  147. default:
  148. case condition::parse_error:
  149. return "A JSON parse error occurred";
  150. case condition::pointer_parse_error:
  151. return "A JSON Pointer parse error occurred";
  152. case condition::pointer_use_error:
  153. return "An error occurred when JSON Pointer was used with"
  154. " a value";
  155. case condition::conversion_error:
  156. return "An error occurred during conversion";
  157. }
  158. }
  159. std::string
  160. error_condition_category_t::message( int cv ) const
  161. {
  162. return message( cv, nullptr, 0 );
  163. }
  164. } // namespace detail
  165. } // namespace json
  166. } // namespace boost
  167. #endif