23 #ifndef FABLE_ERROR_HPP_ 24 #define FABLE_ERROR_HPP_ 29 #include <fmt/format.h> 40 class Error :
public std::exception {
42 explicit Error(
const std::string& what) : err_(what) {}
43 explicit Error(
const char* what) : err_(what) {}
45 template <
typename... Args>
46 explicit Error(
const char* format,
const Args&... args) : err_(fmt::format(format, args...)) {}
48 virtual ~Error() noexcept =
default;
50 const char* what()
const noexcept
override {
return err_.what(); }
53 std::runtime_error err_;
64 template <
typename... Args>
65 ConfError(
const Conf& c,
const char* format,
const Args&... args)
66 :
Error(format, args...), data_(c) {}
68 std::string file()
const {
return data_.
file(); }
69 std::string root()
const {
return data_.
root(); }
70 const Conf& conf()
const {
return data_; }
71 const Json& data()
const {
return *data_; }
73 virtual std::string message()
const {
74 return fmt::format(
"{}:{}: {}", file(), root(), this->what());
78 friend void to_json(
Json& j,
const ConfError& e) {
80 {
"error", e.what()}, {
"file", e.file()}, {
"root", e.root()},
81 {
"data", e.data()}, {
"message", e.message()},
88 inline ConfError MissingProperty(
const Conf& c,
const std::string& key) {
89 return ConfError{c,
"required property missing: {}", key};
92 inline ConfError UnexpectedProperty(
const Conf& c,
const std::string& key) {
93 return ConfError{c,
"unexpected property present: {}", key};
97 std::string want = to_string(t);
98 std::string got = to_string(c->type());
99 return ConfError{c,
"property must have type {}, got {}", want, got};
103 std::string want = to_string(t);
104 std::string got = to_string((*c)[key].type());
105 return ConfError{c,
"property must have type {}, got {}", want, got};
108 inline ConfError WrongType(
const Conf& c,
const std::string& key) {
109 std::string got = to_string((*c)[key].type());
110 return ConfError{c,
"property has wrong type {}", got};
114 std::string got = to_string(c->type());
115 return ConfError{c,
"property has wrong type {}", got};
130 :
ConfError(c), schema_(s), context_(ctx) {}
132 template <
typename... Args>
134 :
ConfError(c, format, args...), schema_(s) {}
136 template <
typename... Args>
137 SchemaError(
const Conf& c,
const Json& s,
const Json& ctx,
const char* format,
139 :
ConfError(c, format, args...), schema_(s), context_(ctx) {}
142 const Json& schema()
const {
return schema_; }
143 const Json& context()
const {
return context_; }
145 friend void to_json(
Json& j,
const SchemaError& e) {
147 {
"error", e.what()}, {
"file", e.file()}, {
"root", e.root()},
148 {
"data", e.data()}, {
"message", e.message()}, {
"schema", e.schema_},
150 if (!e.context_.empty()) {
151 j[
"context"] = e.context_;
158 #endif // FABLE_ERROR_HPP_
std::string root() const
Definition: conf.hpp:123
nlohmann::json Json
Definition: json.hpp:62
Definition: error.hpp:120
Json::value_t JsonType
Definition: json.hpp:78
const std::string & file() const
Definition: conf.hpp:97