50class exception :
public std::exception
54 const char*
what() const noexcept
override
63 JSON_HEDLEY_NON_NULL(3)
64 exception(
int id_, const
char* what_arg) :
id(id_), m(what_arg) {}
66 static std::string name(
const std::string& ename,
int id_)
68 return concat(
"[json.exception.", ename,
'.', std::to_string(id_),
"] ");
71 static std::string diagnostics(std::nullptr_t )
76 template<
typename BasicJsonType>
77 static std::string diagnostics(
const BasicJsonType* leaf_element)
80 std::vector<std::string> tokens;
81 for (
const auto* current = leaf_element; current !=
nullptr && current->m_parent !=
nullptr; current = current->m_parent)
83 switch (current->m_parent->type())
87 for (std::size_t i = 0; i < current->m_parent->m_data.m_value.array->size(); ++i)
89 if (¤t->m_parent->m_data.m_value.array->operator[](i) == current)
91 tokens.emplace_back(std::to_string(i));
100 for (
const auto& element : *current->m_parent->m_data.m_value.object)
102 if (&element.second == current)
104 tokens.emplace_back(element.first.c_str());
129 auto str = std::accumulate(tokens.rbegin(), tokens.rend(), std::string{},
130 [](
const std::string & a,
const std::string & b)
132 return concat(a,
'/', detail::escape(b));
135 return concat(
'(', str,
") ", get_byte_positions(leaf_element));
137 return get_byte_positions(leaf_element);
143 std::runtime_error m;
144#if JSON_DIAGNOSTIC_POSITIONS
145 template<
typename BasicJsonType>
146 static std::string get_byte_positions(
const BasicJsonType* leaf_element)
148 if ((leaf_element->start_pos() != std::string::npos) && (leaf_element->end_pos() != std::string::npos))
150 return concat(
"(bytes ", std::to_string(leaf_element->start_pos()),
"-", std::to_string(leaf_element->end_pos()),
") ");
155 template<
typename BasicJsonType>
156 static std::string get_byte_positions(
const BasicJsonType* leaf_element)
158 static_cast<void>(leaf_element);
166class parse_error :
public exception
178 template<typename BasicJsonContext, enable_if_t<is_basic_json_context<BasicJsonContext>::value,
int> = 0>
179 static parse_error
create(
int id_,
const position_t& pos,
const std::string& what_arg, BasicJsonContext context)
181 const std::string w = concat(exception::name(
"parse_error", id_),
"parse error",
182 position_string(pos),
": ", exception::diagnostics(context), what_arg);
186 template<typename BasicJsonContext, enable_if_t<is_basic_json_context<BasicJsonContext>::value,
int> = 0>
187 static parse_error create(
int id_, std::size_t byte_,
const std::string& what_arg, BasicJsonContext context)
189 const std::string w = concat(exception::name(
"parse_error", id_),
"parse error",
190 (byte_ != 0 ? (concat(
" at byte ", std::to_string(byte_))) :
""),
191 ": ", exception::diagnostics(context), what_arg);
192 return {id_, byte_, w.c_str()};
207 parse_error(
int id_, std::size_t byte_,
const char* what_arg)
208 : exception(id_, what_arg),
byte(byte_) {}
210 static std::string position_string(
const position_t& pos)
212 return concat(
" at line ", std::to_string(pos.
lines_read + 1),
219class invalid_iterator :
public exception
222 template<typename BasicJsonContext, enable_if_t<is_basic_json_context<BasicJsonContext>::value,
int> = 0>
223 static invalid_iterator create(
int id_,
const std::string& what_arg, BasicJsonContext context)
225 const std::string w = concat(exception::name(
"invalid_iterator", id_), exception::diagnostics(context), what_arg);
226 return {id_, w.c_str()};
230 JSON_HEDLEY_NON_NULL(3)
231 invalid_iterator(
int id_,
const char* what_arg)
232 : exception(id_, what_arg) {}
237class type_error :
public exception
240 template<typename BasicJsonContext, enable_if_t<is_basic_json_context<BasicJsonContext>::value,
int> = 0>
241 static type_error create(
int id_,
const std::string& what_arg, BasicJsonContext context)
243 const std::string w = concat(exception::name(
"type_error", id_), exception::diagnostics(context), what_arg);
244 return {id_, w.c_str()};
248 JSON_HEDLEY_NON_NULL(3)
249 type_error(
int id_,
const char* what_arg) : exception(id_, what_arg) {}
254class out_of_range :
public exception
257 template<typename BasicJsonContext, enable_if_t<is_basic_json_context<BasicJsonContext>::value,
int> = 0>
258 static out_of_range create(
int id_,
const std::string& what_arg, BasicJsonContext context)
260 const std::string w = concat(exception::name(
"out_of_range", id_), exception::diagnostics(context), what_arg);
261 return {id_, w.c_str()};
265 JSON_HEDLEY_NON_NULL(3)
266 out_of_range(
int id_,
const char* what_arg) : exception(id_, what_arg) {}
271class other_error :
public exception
274 template<typename BasicJsonContext, enable_if_t<is_basic_json_context<BasicJsonContext>::value,
int> = 0>
275 static other_error create(
int id_,
const std::string& what_arg, BasicJsonContext context)
277 const std::string w = concat(exception::name(
"other_error", id_), exception::diagnostics(context), what_arg);
278 return {id_, w.c_str()};
282 JSON_HEDLEY_NON_NULL(3)
283 other_error(
int id_,
const char* what_arg) : exception(id_, what_arg) {}
static parse_error create(int id_, const position_t &pos, const std::string &what_arg, BasicJsonContext context)
create a parse error exception
定义 exceptions.hpp:179