Gracefully handle non existent options

This commit is contained in:
Alessandro Ranellucci 2016-08-27 18:46:16 +02:00
parent f295d9001a
commit 1e71f438a1
2 changed files with 7 additions and 5 deletions

View File

@ -125,7 +125,7 @@ ConfigBase::serialize(const t_config_option_key &opt_key) const {
bool
ConfigBase::set_deserialize(const t_config_option_key &opt_key, std::string str, bool append) {
const ConfigOptionDef* optdef = this->def->get(opt_key);
if (optdef == NULL) throw std::runtime_error("Calling set_deserialize() on unknown option");
if (optdef == NULL) throw UnknownOptionException();
if (!optdef->shortcut.empty()) {
for (std::vector<t_config_option_key>::const_iterator it = optdef->shortcut.begin(); it != optdef->shortcut.end(); ++it) {
if (!this->set_deserialize(*it, str)) return false;
@ -205,9 +205,8 @@ ConfigBase::load(const std::string &file)
BOOST_FOREACH(const pt::ptree::value_type &v, tree) {
try {
this->set_deserialize(v.first.c_str(), v.second.get_value<std::string>().c_str());
} catch (std::runtime_error& e) {
// skip over errors in the config file but print a warning.
std::cerr << "Caught exception at option " << v.first.c_str() << ".\n";
} catch (UnknownOptionException &e) {
// ignore
}
}
}

View File

@ -5,6 +5,7 @@
#include <climits>
#include <cstdio>
#include <cstdlib>
#include <exception>
#include <iostream>
#include <stdexcept>
#include <string>
@ -633,6 +634,8 @@ class StaticConfig : public virtual ConfigBase
void set_defaults();
};
class UnknownOptionException : public std::exception {};
}
#endif