Horizon
exceptions.hpp
1 #pragma once
2 
3 #include <exception> // exception
4 #include <stdexcept> // runtime_error
5 #include <string> // to_string
6 
7 namespace nlohmann
8 {
9 namespace detail
10 {
12 // exceptions //
14 
43 class exception : public std::exception
44 {
45  public:
47  const char* what() const noexcept override
48  {
49  return m.what();
50  }
51 
53  const int id;
54 
55  protected:
56  exception(int id_, const char* what_arg) : id(id_), m(what_arg) {}
57 
58  static std::string name(const std::string& ename, int id_)
59  {
60  return "[json.exception." + ename + "." + std::to_string(id_) + "] ";
61  }
62 
63  private:
65  std::runtime_error m;
66 };
67 
111 class parse_error : public exception
112 {
113  public:
122  static parse_error create(int id_, std::size_t byte_, const std::string& what_arg)
123  {
124  std::string w = exception::name("parse_error", id_) + "parse error" +
125  (byte_ != 0 ? (" at " + std::to_string(byte_)) : "") +
126  ": " + what_arg;
127  return parse_error(id_, byte_, w.c_str());
128  }
129 
139  const std::size_t byte;
140 
141  private:
142  parse_error(int id_, std::size_t byte_, const char* what_arg)
143  : exception(id_, what_arg), byte(byte_) {}
144 };
145 
184 {
185  public:
186  static invalid_iterator create(int id_, const std::string& what_arg)
187  {
188  std::string w = exception::name("invalid_iterator", id_) + what_arg;
189  return invalid_iterator(id_, w.c_str());
190  }
191 
192  private:
193  invalid_iterator(int id_, const char* what_arg)
194  : exception(id_, what_arg) {}
195 };
196 
235 class type_error : public exception
236 {
237  public:
238  static type_error create(int id_, const std::string& what_arg)
239  {
240  std::string w = exception::name("type_error", id_) + what_arg;
241  return type_error(id_, w.c_str());
242  }
243 
244  private:
245  type_error(int id_, const char* what_arg) : exception(id_, what_arg) {}
246 };
247 
279 class out_of_range : public exception
280 {
281  public:
282  static out_of_range create(int id_, const std::string& what_arg)
283  {
284  std::string w = exception::name("out_of_range", id_) + what_arg;
285  return out_of_range(id_, w.c_str());
286  }
287 
288  private:
289  out_of_range(int id_, const char* what_arg) : exception(id_, what_arg) {}
290 };
291 
316 class other_error : public exception
317 {
318  public:
319  static other_error create(int id_, const std::string& what_arg)
320  {
321  std::string w = exception::name("other_error", id_) + what_arg;
322  return other_error(id_, w.c_str());
323  }
324 
325  private:
326  other_error(int id_, const char* what_arg) : exception(id_, what_arg) {}
327 };
328 }
329 }
nlohmann::detail::type_error
exception indicating executing a member function with a wrong type
Definition: exceptions.hpp:235
nlohmann::detail::exception::what
const char * what() const noexcept override
returns the explanatory string
Definition: exceptions.hpp:47
nlohmann::detail::exception
general exception of the basic_json class
Definition: exceptions.hpp:43
nlohmann::detail::out_of_range
exception indicating access out of the defined range
Definition: exceptions.hpp:279
nlohmann::detail::invalid_iterator
exception indicating errors with iterators
Definition: exceptions.hpp:183
nlohmann
namespace for Niels Lohmann
Definition: adl_serializer.hpp:8
nlohmann::detail::other_error
exception indicating other library errors
Definition: exceptions.hpp:316
nlohmann::detail::parse_error::byte
const std::size_t byte
byte index of the parse error
Definition: exceptions.hpp:139
nlohmann::detail::parse_error
exception indicating a parse error
Definition: exceptions.hpp:111
nlohmann::detail::value_t::string
string value
nlohmann::detail::parse_error::create
static parse_error create(int id_, std::size_t byte_, const std::string &what_arg)
create a parse error exception
Definition: exceptions.hpp:122
nlohmann::detail::exception::id
const int id
the id of the exception
Definition: exceptions.hpp:53