Allow yaml-cpp exception to be caught in the loader and set as error

This allows for better debugging: the exception shows where in the file the error has occurred.
This commit is contained in:
Raphael Coeffic
2021-12-31 10:53:11 +01:00
parent 79181d1484
commit 5628391539
3 changed files with 24 additions and 26 deletions
@@ -83,20 +83,14 @@ bool loadModelsListFromYaml(std::vector<CategoryData>& categories,
bool loadModelFromYaml(ModelData& model, const QByteArray& data) bool loadModelFromYaml(ModelData& model, const QByteArray& data)
{ {
try {
YAML::Node node = loadYamlFromByteArray(data); YAML::Node node = loadYamlFromByteArray(data);
node >> model; node >> model;
} catch (const std::runtime_error& e) {
qDebug() << "YAML::ParserException: " << e.what();
return false;
}
return true; return true;
} }
bool loadRadioSettingsFromYaml(GeneralSettings& settings, const QByteArray& data) bool loadRadioSettingsFromYaml(GeneralSettings& settings, const QByteArray& data)
{ {
try {
YAML::Node node = loadYamlFromByteArray(data); YAML::Node node = loadYamlFromByteArray(data);
node >> settings; node >> settings;
if (settings.version < CPN_CURRENT_SETTINGS_VERSION) { if (settings.version < CPN_CURRENT_SETTINGS_VERSION) {
@@ -107,10 +101,6 @@ bool loadRadioSettingsFromYaml(GeneralSettings& settings, const QByteArray& data
QMessageBox::critical(NULL, CPN_STR_APP_NAME, QCoreApplication::translate("EdgeTXInterface", "Companion does not support settings version %1!").arg(settings.version)); QMessageBox::critical(NULL, CPN_STR_APP_NAME, QCoreApplication::translate("EdgeTXInterface", "Companion does not support settings version %1!").arg(settings.version));
return false; return false;
} }
} catch (const std::runtime_error& e) {
qDebug() << "YAML::ParserException: " << e.what();
return false;
}
return true; return true;
} }
+5 -5
View File
@@ -98,9 +98,12 @@ void operator>>(const YAML::Node& node, T (&value)[N])
if (!node) return; if (!node) return;
if (node.IsMap()) { if (node.IsMap()) {
for (const auto& elmt : node) { for (const auto& elmt : node) {
int idx = -1;
try { try {
int idx = std::stoi(elmt.first.Scalar()); idx = std::stoi(elmt.first.Scalar());
if (idx < 0 || idx >= (int)N) return; } catch (...) {
}
if (idx < 0 || idx >= (int)N) continue;
if (elmt.second.IsMap() && (elmt.second.size() == 1) && if (elmt.second.IsMap() && (elmt.second.size() == 1) &&
elmt.second["val"]) { elmt.second["val"]) {
@@ -108,9 +111,6 @@ void operator>>(const YAML::Node& node, T (&value)[N])
} else { } else {
elmt.second >> value[idx]; elmt.second >> value[idx];
} }
} catch (...) {
return;
}
} }
} else if (node.IsSequence()) { } else if (node.IsSequence()) {
int idx = 0; int idx = 0;
+10 -2
View File
@@ -237,10 +237,15 @@ bool CategorizedStorageFormat::loadYaml(RadioData & radioData)
return false; return false;
} }
try {
if (!loadRadioSettingsFromYaml(radioData.generalSettings, radioSettingsBuffer)) { if (!loadRadioSettingsFromYaml(radioData.generalSettings, radioSettingsBuffer)) {
setError(tr("Can't load RADIO/radio.yml")); setError(tr("Can't load RADIO/radio.yml"));
return false; return false;
} }
} catch(const std::runtime_error& e) {
setError(tr("Can't load RADIO/radio.yml") + ":\n" + QString(e.what()));
return false;
}
board = (Board::Type)radioData.generalSettings.variant; board = (Board::Type)radioData.generalSettings.variant;
@@ -249,8 +254,6 @@ bool CategorizedStorageFormat::loadYaml(RadioData & radioData)
QByteArray modelslistBuffer; QByteArray modelslistBuffer;
if (loadFile(modelslistBuffer, "MODELS/models.yml")) { if (loadFile(modelslistBuffer, "MODELS/models.yml")) {
if (!loadModelsListFromYaml(radioData.categories, modelFiles, modelslistBuffer)) { if (!loadModelsListFromYaml(radioData.categories, modelFiles, modelslistBuffer)) {
setError(tr("Can't load MODELS/models.yml")); setError(tr("Can't load MODELS/models.yml"));
return false; return false;
@@ -292,10 +295,15 @@ bool CategorizedStorageFormat::loadYaml(RadioData & radioData)
// //
auto& model = radioData.models[mc.modelIdx]; auto& model = radioData.models[mc.modelIdx];
try {
if (!loadModelFromYaml(model,modelBuffer)) { if (!loadModelFromYaml(model,modelBuffer)) {
setError(tr("Can't load ") + filename); setError(tr("Can't load ") + filename);
return false; return false;
} }
} catch(const std::runtime_error& e) {
setError(tr("Can't load ") + filename + ":\n" + QString(e.what()));
return false;
}
model.category = mc.category; model.category = mc.category;
model.modelIndex = mc.modelIdx; model.modelIndex = mc.modelIdx;