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 UnexpectedProperty(
const Conf& conf,
const std::string& key) {
103 return ConfError{conf,
"unexpected property present: {}", key};
106 inline ConfError WrongType(
const Conf& conf, JsonType type) {
107 std::string want = to_string(type);
108 std::string got = to_string(conf->type());
109 return ConfError{conf,
"property must have type {}, got {}", want, got};
112 inline ConfError WrongType(
const Conf& conf,
const std::string& key, JsonType type) {
113 std::string want = to_string(type);
114 std::string got = to_string((*conf)[key].type());
115 return ConfError{conf,
"property must have type {}, got {}", want, got};
118 inline ConfError WrongType(
const Conf& conf,
const std::string& key) {
119 std::string got = to_string((*conf)[key].type());
120 return ConfError{conf,
"property has wrong type {}", got};
123 inline ConfError WrongType(
const Conf& conf) {
124 std::string got = to_string(conf->type());
125 return ConfError{conf,
"property has wrong type {}", got};
158 template <
typename... Args>
160 :
ConfError(conf, format, std::forward<Args>(args)...), schema_(std::move(schema)) {}
162 [[nodiscard]]
const Json& schema()
const {
return schema_; }
163 [[nodiscard]]
const Json& context()
const {
return context_; }
165 SchemaError& with_context(Json ctx) {
166 context_ = std::move(ctx);
170 friend void to_json(Json& json,
const SchemaError& err) {
172 {
"error", err.what()}, {
"file", err.file()}, {
"root", err.root()},
173 {
"data", err.data()}, {
"message", err.message()}, {
"schema", err.schema_},
175 if (!err.context_.empty()) {
176 json[
"context"] = err.context_;
std::string root() const
Definition: conf.hpp:125
const std::string & file() const
Definition: conf.hpp:99
Definition: error.hpp:130
SchemaError(const ConfError &err, Json schema)
Definition: error.hpp:148
SchemaError(const Conf &conf, Json schema, std::string_view format, Args &&... args)
Definition: error.hpp:159
nlohmann::json Json
Definition: fable_fwd.hpp:35