cpp-hocon  0.3.0
config_exception.hpp
1 #pragma once
2 
3 #include <stdexcept>
4 #include <string>
5 #include "config_origin.hpp"
6 #include <leatherman/locale/locale.hpp>
7 
8 namespace hocon {
9 
14  struct config_exception : public std::runtime_error {
15  config_exception(config_origin const& origin, std::string const& message) :
16  runtime_error(leatherman::locale::format("{1}: {2}", origin.description(), message)) { }
17  config_exception(std::string const& message) : runtime_error(message) { }
18 
19  config_exception(std::string const& message, std::exception const& e) : runtime_error(leatherman::locale::format("{1} {2}", message, e.what())) { }
20  };
21 
28  wrong_type_exception(config_origin const& origin,
29  std::string const& path, std::string const& expected, std::string const& actual) :
30  config_exception(origin, leatherman::locale::format("{1} has type {2} rather than {3}", path, actual, expected)) { }
31  using config_exception::config_exception;
32  };
33 
39  missing_exception(std::string const& path) :
40  config_exception(leatherman::locale::format("No configuration setting found for key '{1}'", path)) { }
41  using config_exception::config_exception;
42  };
43 
49  null_exception(config_origin const& origin, std::string const& path, std::string const& expected = "") :
50  missing_exception(origin, (expected.empty() ? leatherman::locale::format("Configuration key \"{1}\" is null", path)
51  : leatherman::locale::format("Configuration key \"{1}\" is set to null but expected {2}", path, expected))) { }
52  };
53 
60  bad_value_exception(config_origin const& origin, std::string const& path, std::string const& message) :
61  config_exception(origin, leatherman::locale::format("Invalid value at '{1}': {2}", path, message)) { }
62  bad_value_exception(std::string const& path, std::string const& message) :
63  config_exception(leatherman::locale::format("Invalid value at '{1}': {2}", path, message)) { }
64  };
65 
72  bad_path_exception(config_origin const& origin, std::string const& path, std::string const& message) :
73  config_exception(origin, path.empty() ? message : leatherman::locale::format("Invalid path '{1}': {2}", path, message)) { }
74  bad_path_exception(std::string const& path, std::string const& message) :
75  config_exception(path.empty() ? message : leatherman::locale::format("Invalid path '{1}': {2}", path, message)) { }
76  };
77 
86  bug_or_broken_exception(std::string const& message) : config_exception(message) { }
87  };
88 
93  struct io_exception : public config_exception {
94  io_exception(config_origin const& origin, std::string const& message) : config_exception(origin, message) { }
95  };
96 
102  parse_exception(config_origin const& origin, std::string const& message) : config_exception(origin, message) { }
103  };
104 
105 
111  unresolved_substitution_exception(config_origin const& origin, std::string const& detail) :
112  parse_exception(origin, leatherman::locale::format("Could not resolve subtitution to a value: {1}", detail)) { }
113  };
114 
124  not_resolved_exception(std::string const& message) : bug_or_broken_exception(message) { }
125  };
126 
128  not_possible_to_resolve_exception(std::string const& message) : bug_or_broken_exception(message) { }
129  };
130 
137  validation_problem(std::string path_, shared_origin origin_, std::string problem_) :
138  path(std::move(path_)), origin(std::move(origin_)), problem(std::move(problem_)) { }
139 
140  const std::string path;
141  const shared_origin origin;
142  const std::string problem;
143 
144  std::string to_string() {
145  return leatherman::locale::format("ValidationProblem({1},{2},{3})", path, origin->description(), problem);
146  }
147  };
148 
156  validation_failed_exception(std::vector<validation_problem> problems_) :
157  config_exception(make_message(problems_)), problems(std::move(problems_)) { }
158 
159  const std::vector<validation_problem> problems;
160 
161  private:
162  static std::string make_message(std::vector<validation_problem> const& problems_) {
163  std::string msg;
164  for (auto &p : problems_) {
165  auto sep = std::string(": ");
166  for (auto &s : {p.origin->description(), sep, p.path, sep, p.problem, std::string(", ")}) {
167  msg.append(s);
168  }
169  }
170  if (msg.empty()) {
171  throw bug_or_broken_exception(leatherman::locale::format("validation_failed_exception must have a non-empty list of problems"));
172  }
173  msg.resize(msg.length() - 2);
174  return msg;
175  }
176  };
177 
182  generic_exception(std::string const& message) : config_exception(message) { }
183  };
184 } // namespace hocon
hocon::not_resolved_exception
Exception indicating that you tried to use a function that requires substitutions to be resolved,...
Definition: config_exception.hpp:123
hocon
Factory for creating config_document instances.
Definition: config.hpp:18
hocon::validation_failed_exception
Exception indicating that config#check_valid found validity problems.
Definition: config_exception.hpp:155
hocon::validation_problem
Information about a problem that occurred in config#check_valid.
Definition: config_exception.hpp:136
hocon::io_exception
Exception indicating that there was an IO error.
Definition: config_exception.hpp:93
hocon::bad_value_exception
Exception indicating that a value was messed up, for example you may have asked for a duration and th...
Definition: config_exception.hpp:59
hocon::bug_or_broken_exception
Exception indicating that there's a bug in something (possibly the library itself) or the runtime env...
Definition: config_exception.hpp:85
hocon::generic_exception
Exception that doesn't fall into any other category.
Definition: config_exception.hpp:181
hocon::unresolved_substitution_exception
Exception indicating that a substitution did not resolve to anything.
Definition: config_exception.hpp:110
hocon::bad_path_exception
Exception indicating that a path expression was invalid.
Definition: config_exception.hpp:71
hocon::config_origin::description
virtual LIBCPP_HOCON_EXPORT std::string const & description() const =0
Returns a string describing the origin of a value or exception.
hocon::path
Definition: path.hpp:13
hocon::parse_exception
Exception indicating that there was a parse error.
Definition: config_exception.hpp:101
hocon::wrong_type_exception
Exception indicating that the type of a value does not match the type you requested.
Definition: config_exception.hpp:27
hocon::not_possible_to_resolve_exception
Definition: config_exception.hpp:127
hocon::config_exception
All exceptions thrown by the library are subclasses of config_exception.
Definition: config_exception.hpp:14
hocon::missing_exception
Exception indicates that the setting was never set to anything, not even null.
Definition: config_exception.hpp:38
hocon::problem
Definition: tokens.hpp:51
hocon::config_origin
Represents the origin (such as filename and line number) of a config_value for use in error messages.
Definition: config_origin.hpp:31
hocon::null_exception
Exception indicates that the setting was treated as missing because it was set to null.
Definition: config_exception.hpp:48