Skip to content

Commit

Permalink
Gracefully handle non existent options
Browse files Browse the repository at this point in the history
  • Loading branch information
alranel committed Aug 27, 2016
1 parent f295d90 commit 1e71f43
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 5 deletions.
9 changes: 4 additions & 5 deletions xs/src/libslic3r/Config.cpp
Expand Up @@ -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;
Expand Down Expand Up @@ -203,11 +203,10 @@ ConfigBase::load(const std::string &file)
pt::ptree tree;
pt::read_ini(file, 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());
} 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
}
}
}
Expand Down
3 changes: 3 additions & 0 deletions xs/src/libslic3r/Config.hpp
Expand Up @@ -5,6 +5,7 @@
#include <climits>
#include <cstdio>
#include <cstdlib>
#include <exception>
#include <iostream>
#include <stdexcept>
#include <string>
Expand Down Expand Up @@ -633,6 +634,8 @@ class StaticConfig : public virtual ConfigBase
void set_defaults();
};

class UnknownOptionException : public std::exception {};

}

#endif

0 comments on commit 1e71f43

Please sign in to comment.