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 
118 class parse_error : public exception
119 {
120  public:
130  static parse_error create(int id_, const position_t& pos, const std::string& what_arg)
131  {
132  std::string w = exception::name("parse_error", id_) + "parse error" +
133  position_string(pos) + ": " + what_arg;
134  return parse_error(id_, pos.chars_read_total, w.c_str());
135  }
136 
137  static parse_error create(int id_, std::size_t byte_, const std::string& what_arg)
138  {
139  std::string w = exception::name("parse_error", id_) + "parse error" +
140  (byte_ != 0 ? (" at byte " + std::to_string(byte_)) : "") +
141  ": " + what_arg;
142  return parse_error(id_, byte_, w.c_str());
143  }
144 
154  const std::size_t byte;
155 
156  private:
157  parse_error(int id_, std::size_t byte_, const char* what_arg)
158  : exception(id_, what_arg), byte(byte_) {}
159 
160  static std::string position_string(const position_t& pos)
161  {
162  return " at line " + std::to_string(pos.lines_read + 1) +
163  ", column " + std::to_string(pos.chars_read_current_line);
164  }
165 };
166 
205 {
206  public:
207  static invalid_iterator create(int id_, const std::string& what_arg)
208  {
209  std::string w = exception::name("invalid_iterator", id_) + what_arg;
210  return invalid_iterator(id_, w.c_str());
211  }
212 
213  private:
214  JSON_HEDLEY_NON_NULL(3)
215  invalid_iterator(int id_, const char* what_arg)
216  : exception(id_, what_arg) {}
217 };
218 
258 class type_error : public exception
259 {
260  public:
261  static type_error create(int id_, const std::string& what_arg)
262  {
263  std::string w = exception::name("type_error", id_) + what_arg;
264  return type_error(id_, w.c_str());
265  }
266 
267  private:
268  JSON_HEDLEY_NON_NULL(3)
269  type_error(int id_, const char* what_arg) : exception(id_, what_arg) {}
270 };
271 
305 class out_of_range : public exception
306 {
307  public:
308  static out_of_range create(int id_, const std::string& what_arg)
309  {
310  std::string w = exception::name("out_of_range", id_) + what_arg;
311  return out_of_range(id_, w.c_str());
312  }
313 
314  private:
315  JSON_HEDLEY_NON_NULL(3)
316  out_of_range(int id_, const char* what_arg) : exception(id_, what_arg) {}
317 };
318 
343 class other_error : public exception
344 {
345  public:
346  static other_error create(int id_, const std::string& what_arg)
347  {
348  std::string w = exception::name("other_error", id_) + what_arg;
349  return other_error(id_, w.c_str());
350  }
351 
352  private:
353  JSON_HEDLEY_NON_NULL(3)
354  other_error(int id_, const char* what_arg) : exception(id_, what_arg) {}
355 };
356 } // namespace detail
357 } // namespace nlohmann
general exception of the basic_json class
Definition: exceptions.hpp:47
const int id
the id of the exception
Definition: exceptions.hpp:57
JSON_HEDLEY_RETURNS_NON_NULL const char * what() const noexcept override
returns the explanatory string
Definition: exceptions.hpp:51
exception indicating errors with iterators
Definition: exceptions.hpp:205
exception indicating other library errors
Definition: exceptions.hpp:344
exception indicating access out of the defined range
Definition: exceptions.hpp:306
exception indicating a parse error
Definition: exceptions.hpp:119
static parse_error create(int id_, const position_t &pos, const std::string &what_arg)
create a parse error exception
Definition: exceptions.hpp:130
const std::size_t byte
byte index of the parse error
Definition: exceptions.hpp:154
exception indicating executing a member function with a wrong type
Definition: exceptions.hpp:259
namespace for Niels Lohmann
Definition: adl_serializer.hpp:9
struct to capture the start position of the current token
Definition: position_t.hpp:11
std::size_t lines_read
the number of lines read
Definition: position_t.hpp:17
std::size_t chars_read_current_line
the number of characters read in the current line
Definition: position_t.hpp:15
std::size_t chars_read_total
the total number of characters read
Definition: position_t.hpp:13