Skip to content

Commit

Permalink
Improved error handling while loading config
Browse files Browse the repository at this point in the history
  • Loading branch information
Masahiro Tanaka committed Oct 7, 2022
1 parent 3468769 commit 16114f3
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 15 deletions.
42 changes: 29 additions & 13 deletions src/Config.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -217,6 +217,12 @@ Config::Config() {
throw std::invalid_argument("Config dir not found: " + conf_dir.string());
}

if (!fs::is_directory(conf_dir)) {
throw std::invalid_argument(
"The path specified as the config dir is not a directory: " +
conf_dir.string());
}

conf_file = conf_dir / RANNC_CONF_FILE;
} else {
const fs::path home_dir = getHomeDir();
Expand All @@ -225,23 +231,33 @@ Config::Config() {
}

if (fs::exists(conf_file)) {
values_[CONF_DIR] = conf_dir.string();
if (fs::is_directory(conf_file)) {
std::cerr
<< "The path of the config file is a directory. Skipping loading config: "
<< conf_file << std::endl;
} else {
values_[CONF_DIR] = conf_dir.string();

const auto data = toml::parse(conf_file.string());
for (const auto& it : data.as_table()) {
// for(const auto& [name, val] : data.as_table())
try {
const auto data = toml::parse(conf_file.string());
for (const auto& it : data.as_table()) {
const std::string& name = it.first;
toml::value val = it.second;

const std::string& name = it.first;
toml::value val = it.second;
if (!contains(items_, name)) {
std::cerr << "Ignored unknown item in " << conf_file << ": " << name
<< std::endl;
continue;
}

if (!contains(items_, name)) {
std::cerr << "Ignored unknown item in " << conf_file << ": " << name
<< std::endl;
continue;
const auto& item = items_.at(name);
values_[name] = convertValue(val, item.type);
}
} catch (const std::exception& e) {
std::stringstream ss;
ss << "Failed to parse config file: " << conf_file;
throw std::invalid_argument(ss.str());
}

const auto& item = items_.at(name);
values_[name] = convertValue(val, item.type);
}
}

Expand Down
9 changes: 7 additions & 2 deletions src/Config.h
Original file line number Diff line number Diff line change
Expand Up @@ -172,8 +172,13 @@ class Config {
Config& operator=(Config&&) = delete;

static Config& get() {
static Config instance;
return instance;
try {
static Config instance;
return instance;
} catch (const std::exception& e) {
std::cerr << "Error: " << e.what() << std::endl;
std::exit(1);
}
}

template <typename T>
Expand Down

0 comments on commit 16114f3

Please sign in to comment.