Config: Handle unknown system strings better

Currently, a simple typo in the system name will trigger an assert
message that complains about a "programming error". This is not
user friendly and misleading.

So this changes GetSystemFromName to return an std::optional, which
allows for callers to check whether the system exists and handle
failures better.
This commit is contained in:
Léo Lam
2017-11-26 18:24:01 +01:00
parent 653977cec7
commit 05c8d229af
4 changed files with 14 additions and 8 deletions

View File

@ -3,6 +3,7 @@
// Refer to the license.txt file included.
#include <list>
#include <optional>
#include <sstream>
#include <string>
#include <tuple>
@ -40,8 +41,12 @@ public:
std::getline(buffer, section, '.');
std::getline(buffer, key, '=');
std::getline(buffer, value, '=');
Config::System system = Config::GetSystemFromName(system_str);
m_values.emplace_back(std::make_tuple(Config::ConfigLocation{system, section, key}, value));
const std::optional<Config::System> system = Config::GetSystemFromName(system_str);
if (system)
{
m_values.emplace_back(
std::make_tuple(Config::ConfigLocation{*system, section, key}, value));
}
}
}