0
0
Fork 0
mirror of https://github.com/matrix-construct/construct synced 2024-06-11 06:28:55 +02:00

ircd::conf: Add more elaborate double-fault handling.

This commit is contained in:
Jason Volk 2019-03-11 12:16:32 -07:00
parent 9930d2ce8f
commit d474eed948
2 changed files with 73 additions and 1 deletions

View file

@ -64,6 +64,8 @@ namespace ircd::conf
std::string get(const string_view &key);
bool set(const string_view &key, const string_view &value);
bool set(std::nothrow_t, const string_view &key, const string_view &value);
bool fault(std::nothrow_t, const string_view &key) noexcept;
void fault(const string_view &key);
bool reset(std::nothrow_t, const string_view &key);
bool reset(const string_view &key);
size_t reset();
@ -91,6 +93,7 @@ struct ircd::conf::item<void>
string_view get(const mutable_buffer &) const;
std::string get() const;
bool set(const string_view &);
void fault() noexcept;
item(const json::members &, conf::set_cb);
item(item &&) = delete;

View file

@ -68,6 +68,35 @@ catch(const std::exception &e)
throw;
}
void
ircd::conf::fault(const string_view &key)
try
{
auto &item(*items.at(key));
item.fault();
}
catch(const std::out_of_range &e)
{
throw not_found
{
"Conf item '%s' is not available", key
};
}
bool
ircd::conf::fault(std::nothrow_t,
const string_view &key)
noexcept try
{
auto &item(*items.at(key));
item.fault();
return true;
}
catch(const std::out_of_range &e)
{
return false;
}
bool
ircd::conf::set(std::nothrow_t,
const string_view &key,
@ -218,6 +247,36 @@ noexcept
}
}
void
ircd::conf::item<void>::fault()
noexcept try
{
const json::string &default_
{
feature.get("default")
};
log::warning
{
"conf item[%s] defaulting with featured value :%s",
name,
string_view{default_}
};
if(on_set(default_))
if(set_cb)
set_cb();
}
catch(const std::exception &e)
{
throw panic
{
"Conf item '%s' failed to set its default value (double-fault) :%s",
name,
e.what()
};
}
bool
ircd::conf::item<void>::set(const string_view &val)
{
@ -229,7 +288,17 @@ ircd::conf::item<void>::set(const string_view &val)
}
catch(...)
{
on_set(existing);
try
{
if(on_set(existing))
if(set_cb)
set_cb();
}
catch(...)
{
fault();
}
throw;
}