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

View File

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