1 #ifndef SIMDJSON_INLINE_ERROR_H
2 #define SIMDJSON_INLINE_ERROR_H
4 #include "simdjson/error.h"
7 namespace simdjson::internal {
9 struct error_code_info {
14 inline const error_code_info error_codes[] {
15 { SUCCESS,
"No error" },
16 { SUCCESS_AND_HAS_MORE,
"No error and buffer still has more data" },
17 { CAPACITY,
"This parser can't support a document that big" },
18 { MEMALLOC,
"Error allocating memory, we're most likely out of memory" },
19 { TAPE_ERROR,
"Something went wrong while writing to the tape" },
20 { DEPTH_ERROR,
"The JSON document was too deep (too many nested objects and arrays)" },
21 { STRING_ERROR,
"Problem while parsing a string" },
22 { T_ATOM_ERROR,
"Problem while parsing an atom starting with the letter 't'" },
23 { F_ATOM_ERROR,
"Problem while parsing an atom starting with the letter 'f'" },
24 { N_ATOM_ERROR,
"Problem while parsing an atom starting with the letter 'n'" },
25 { NUMBER_ERROR,
"Problem while parsing a number" },
26 { UTF8_ERROR,
"The input is not valid UTF-8" },
27 { UNINITIALIZED,
"Uninitialized" },
28 { EMPTY,
"Empty: no JSON found" },
29 { UNESCAPED_CHARS,
"Within strings, some characters must be escaped, we found unescaped characters" },
30 { UNCLOSED_STRING,
"A string is opened, but never closed." },
31 { UNSUPPORTED_ARCHITECTURE,
"simdjson does not have an implementation supported by this CPU architecture (perhaps it's a non-SIMD CPU?)." },
32 { INCORRECT_TYPE,
"The JSON element does not have the requested type." },
33 { NUMBER_OUT_OF_RANGE,
"The JSON number is too large or too small to fit within the requested type." },
34 { INDEX_OUT_OF_BOUNDS,
"Attempted to access an element of a JSON array that is beyond its length." },
35 { NO_SUCH_FIELD,
"The JSON field referenced does not exist in this object." },
36 { IO_ERROR,
"Error reading the file." },
37 { INVALID_JSON_POINTER,
"Invalid JSON pointer syntax." },
38 { INVALID_URI_FRAGMENT,
"Invalid URI fragment syntax." },
39 { UNEXPECTED_ERROR,
"Unexpected error, consider reporting this problem as you may have found a bug in simdjson" }
45 inline const char *error_message(error_code error) noexcept {
47 return internal::error_codes[int(error)].message.c_str();
50 inline const std::string &error_message(
int error) noexcept {
51 if (error < 0 || error >= error_code::NUM_ERROR_CODES) {
52 return internal::error_codes[UNEXPECTED_ERROR].message;
54 return internal::error_codes[error].message;
57 inline std::ostream& operator<<(std::ostream& out, error_code error) noexcept {
58 return out << error_message(error);
68 really_inline
void simdjson_result_base<T>::tie(T &value, error_code &error) && noexcept {
72 value = std::forward<simdjson_result_base<T>>(*this).first;
77 really_inline error_code simdjson_result_base<T>::error() const noexcept {
81 #if SIMDJSON_EXCEPTIONS
84 really_inline T& simdjson_result_base<T>::value() noexcept(false) {
85 if (error()) {
throw simdjson_error(error()); }
90 really_inline T&& simdjson_result_base<T>::take_value() && noexcept(
false) {
91 if (error()) {
throw simdjson_error(error()); }
92 return std::forward<T>(this->first);
96 really_inline simdjson_result_base<T>::operator T&&() && noexcept(
false) {
97 return std::forward<simdjson_result_base<T>>(*this).take_value();
100 #endif // SIMDJSON_EXCEPTIONS
103 really_inline simdjson_result_base<T>::simdjson_result_base(T &&value, error_code error) noexcept
104 : std::pair<T, error_code>(std::forward<T>(value), error) {}
106 really_inline simdjson_result_base<T>::simdjson_result_base(error_code error) noexcept
107 : simdjson_result_base(T{}, error) {}
109 really_inline simdjson_result_base<T>::simdjson_result_base(T &&value) noexcept
110 : simdjson_result_base(std::forward<T>(value), SUCCESS) {}
112 really_inline simdjson_result_base<T>::simdjson_result_base() noexcept
113 : simdjson_result_base(T{}, UNINITIALIZED) {}
123 std::forward<internal::simdjson_result_base<T>>(*this).tie(value, error);
128 return internal::simdjson_result_base<T>::error();
131 #if SIMDJSON_EXCEPTIONS
135 return internal::simdjson_result_base<T>::value();
140 return std::forward<internal::simdjson_result_base<T>>(*this).take_value();
145 return std::forward<internal::simdjson_result_base<T>>(*this).
take_value();
148 #endif // SIMDJSON_EXCEPTIONS
152 : internal::simdjson_result_base<T>(std::forward<T>(value), error) {}
154 really_inline simdjson_result<T>::simdjson_result(error_code error) noexcept
155 : internal::simdjson_result_base<T>(error) {}
157 really_inline simdjson_result<T>::simdjson_result(T &&value) noexcept
158 : internal::simdjson_result_base<T>(std::forward<T>(value)) {}
160 really_inline simdjson_result<T>::simdjson_result() noexcept
161 : internal::simdjson_result_base<T>() {}
165 #endif // SIMDJSON_INLINE_ERROR_H