Horizon
exceptions.hpp
1 #pragma once
2 
3 #include <exception> // exception
4 #include <stdexcept> // runtime_error
5 #include <string> // to_string
6 
7 #include <nlohmann/detail/input/position_t.hpp>
8 #include <nlohmann/detail/macro_scope.hpp>
9 
10 namespace nlohmann
11 {
12 namespace detail
13 {
15 // exceptions //
17 
46 class exception : public std::exception
47 {
48  public:
50  JSON_HEDLEY_RETURNS_NON_NULL
51  const char* what() const noexcept override
52  {
53  return m.what();
54  }
55 
57  const int id;
58 
59  protected:
60  JSON_HEDLEY_NON_NULL(3)
61  exception(int id_, const char* what_arg) : id(id_), m(what_arg) {}
62 
63  static std::string name(const std::string& ename, int id_)
64  {
65  return "[json.exception." + ename + "." + std::to_string(id_) + "] ";
66  }
67 
68  private:
70  std::runtime_error m;
71 };
72 
117 class parse_error : public exception
118 {
119  public:
129  static parse_error create(int id_, const position_t& pos, const std::string& what_arg)
130  {
131  std::string w = exception::name("parse_error", id_) + "parse error" +
132  position_string(pos) + ": " + what_arg;
133  return parse_error(id_, pos.chars_read_total, w.c_str());
134  }
135 
136  static parse_error create(int id_, std::size_t byte_, const std::string& what_arg)
137  {
138  std::string w = exception::name("parse_error", id_) + "parse error" +
139  (byte_ != 0 ? (" at byte " + std::to_string(byte_)) : "") +
140  ": " + what_arg;
141  return parse_error(id_, byte_, w.c_str());
142  }
143 
153  const std::size_t byte;
154 
155  private:
156  parse_error(int id_, std::size_t byte_, const char* what_arg)
157  : exception(id_, what_arg), byte(byte_) {}
158 
159  static std::string position_string(const position_t& pos)
160  {
161  return " at line " + std::to_string(pos.lines_read + 1) +
162  ", column " + std::to_string(pos.chars_read_current_line);
163  }
164 };
165 
203 class invalid_iterator : public exception
204 {
205  public:
206  static invalid_iterator create(int id_, const std::string& what_arg)
207  {
208  std::string w = exception::name("invalid_iterator", id_) + what_arg;
209  return invalid_iterator(id_, w.c_str());
210  }
211 
212  private:
213  JSON_HEDLEY_NON_NULL(3)
214  invalid_iterator(int id_, const char* what_arg)
215  : exception(id_, what_arg) {}
216 };
217 
257 class type_error : public exception
258 {
259  public:
260  static type_error create(int id_, const std::string& what_arg)
261  {
262  std::string w = exception::name("type_error", id_) + what_arg;
263  return type_error(id_, w.c_str());
264  }
265 
266  private:
267  JSON_HEDLEY_NON_NULL(3)
268  type_error(int id_, const char* what_arg) : exception(id_, what_arg) {}
269 };
270 
304 class out_of_range : public exception
305 {
306  public:
307  static out_of_range create(int id_, const std::string& what_arg)
308  {
309  std::string w = exception::name("out_of_range", id_) + what_arg;
310  return out_of_range(id_, w.c_str());
311  }
312 
313  private:
314  JSON_HEDLEY_NON_NULL(3)
315  out_of_range(int id_, const char* what_arg) : exception(id_, what_arg) {}
316 };
317 
342 class other_error : public exception
343 {
344  public:
345  static other_error create(int id_, const std::string& what_arg)
346  {
347  std::string w = exception::name("other_error", id_) + what_arg;
348  return other_error(id_, w.c_str());
349  }
350 
351  private:
352  JSON_HEDLEY_NON_NULL(3)
353  other_error(int id_, const char* what_arg) : exception(id_, what_arg) {}
354 };
355 } // namespace detail
356 } // namespace nlohmann
nlohmann::detail::exception
general exception of the basic_json class
Definition: exceptions.hpp:47
nlohmann::detail::position_t
struct to capture the start position of the current token
Definition: position_t.hpp:11
nlohmann
namespace for Niels Lohmann
Definition: adl_serializer.hpp:9
nlohmann::detail::exception::what
JSON_HEDLEY_RETURNS_NON_NULL const char * what() const noexcept override
returns the explanatory string
Definition: exceptions.hpp:51
nlohmann::detail::parse_error::byte
const std::size_t byte
byte index of the parse error
Definition: exceptions.hpp:153
nlohmann::detail::parse_error
exception indicating a parse error
Definition: exceptions.hpp:118
nlohmann::detail::exception::id
const int id
the id of the exception
Definition: exceptions.hpp:57
nlohmann::detail::parse_error::create
static parse_error create(int id_, const position_t &pos, const std::string &what_arg)
create a parse error exception
Definition: exceptions.hpp:129
nlohmann::detail::position_t::chars_read_total
std::size_t chars_read_total
the total number of characters read
Definition: position_t.hpp:13