28 #include <fmt/format.h>
40 class Error :
public std::exception {
49 ~
Error() noexcept
override =
default;
51 Error(std::string what) : message_(std::move(what)) {}
53 template <
typename... Args>
54 explicit Error(std::string_view format, Args&&... args)
55 : message_(fmt::format(fmt::runtime(format), std::forward<Args>(args)...)) {}
57 [[nodiscard]]
const char* what()
const noexcept
override {
return message_.c_str(); }
74 template <
typename... Args>
76 :
Error(format, std::forward<Args>(args)...), data_(std::move(conf)) {}
78 [[nodiscard]] std::string file()
const noexcept {
return data_.
file(); }
79 [[nodiscard]] std::string root()
const noexcept {
return data_.
root(); }
80 [[nodiscard]]
const Conf& conf()
const noexcept {
return data_; }
81 [[nodiscard]]
const Json& data()
const noexcept {
return *data_; }
83 [[nodiscard]]
virtual std::string message()
const {
84 return fmt::format(
"{}:{}: {}", file(), root(), this->what());
90 {
"error", err.what()}, {
"file", err.file()}, {
"root", err.root()},
91 {
"data", err.data()}, {
"message", err.message()},
98 inline ConfError MissingProperty(
const Conf& conf,
const std::string& key) {
99 return ConfError{conf,
"required property missing: {}", key};
102 inline ConfError MissingProperty(
const Conf& conf,
const JsonPointer& ptr) {
103 return ConfError{conf,
"required property missing: {}", ptr.to_string()};
106 inline ConfError UnexpectedProperty(
const Conf& conf,
const std::string& key) {
107 return ConfError{conf,
"unexpected property present: {}", key};
110 inline ConfError UnexpectedProperty(
const Conf& conf,
const JsonPointer& ptr) {
111 return ConfError{conf,
"unexpected property present: {}", ptr.to_string()};
114 inline ConfError WrongType(
const Conf& conf, JsonType type) {
115 std::string want = to_string(type);
116 std::string got = to_string(conf->type());
117 return ConfError{conf,
"property must have type {}, got {}", want, got};
120 inline ConfError WrongType(
const Conf& conf,
const std::string& key, JsonType expected) {
121 std::string want = to_string(expected);
122 std::string got = to_string((*conf)[key].type());
123 return ConfError{conf,
"property '{}' must have type {}, got {}", key, want, got};
126 inline ConfError WrongType(
const Conf& conf,
const JsonPointer& ptr, JsonType expected) {
127 std::string want = to_string(expected);
128 std::string got = to_string((*conf)[ptr].type());
129 return ConfError{conf,
"property '{}' must have type {}, got {}", ptr.to_string(), want, got};
132 inline ConfError WrongType(
const Conf& conf,
const std::string& key) {
133 std::string got = to_string((*conf)[key].type());
134 return ConfError{conf,
"property '{}' has wrong type {}", key, got};
137 inline ConfError WrongType(
const Conf& conf,
const JsonPointer& ptr) {
138 std::string got = to_string((*conf)[ptr].type());
139 return ConfError{conf,
"property '{}' has wrong type {}", ptr.to_string(), got};
142 inline ConfError WrongType(
const Conf& conf) {
143 std::string got = to_string(conf->type());
144 return ConfError{conf,
"property has wrong type {}", got};
177 template <
typename... Args>
179 :
ConfError(conf, format, std::forward<Args>(args)...), schema_(std::move(schema)) {}
181 [[nodiscard]]
const Json& schema()
const {
return schema_; }
182 [[nodiscard]]
const Json& context()
const {
return context_; }
184 SchemaError& with_context(Json ctx) {
185 context_ = std::move(ctx);
189 friend void to_json(Json& json,
const SchemaError& err) {
191 {
"error", err.what()}, {
"file", err.file()}, {
"root", err.root()},
192 {
"data", err.data()}, {
"message", err.message()}, {
"schema", err.schema_},
194 if (!err.context_.empty()) {
195 json[
"context"] = err.context_;
std::string root() const
Definition: conf.hpp:153
const std::string & file() const
Definition: conf.hpp:104
Definition: error.hpp:149
SchemaError(const ConfError &err, Json schema)
Definition: error.hpp:167
SchemaError(const Conf &conf, Json schema, std::string_view format, Args &&... args)
Definition: error.hpp:178
nlohmann::json Json
Definition: fable_fwd.hpp:35