From 6ea311761df5548f18c0af2f04a81ffe662ae49c Mon Sep 17 00:00:00 2001 From: samamou Date: Thu, 12 Dec 2024 12:13:17 -0500 Subject: [PATCH 1/2] improve error handling --- src/puara_spiffs.cpp | 25 ++++++++++++++++++++++--- 1 file changed, 22 insertions(+), 3 deletions(-) diff --git a/src/puara_spiffs.cpp b/src/puara_spiffs.cpp index cbe4f4d..b070dfd 100644 --- a/src/puara_spiffs.cpp +++ b/src/puara_spiffs.cpp @@ -90,12 +90,31 @@ void SPIFFS::unmount_spiffs() // Can be improved double JSONSettings::getVarNumber(std::string varName) { - return variables.at(variables_fields.at(varName)).numberValue; + auto field = variables_fields.find(varName); + if(field == variables_fields.end()) + { + return 0; + } + if(*field >= variables.size() || *field < 0) + { + return 0; + } + return variables[*field].numberValue; } std::string JSONSettings::getVarText(std::string varName) { - return variables.at(variables_fields.at(varName)).textValue; + auto field = variables_fields.find(varName); + if(field == variables_fields.end()) + { + return ""; + } + if(*field >= variables.size() || *field < 0) + { + return ""; + } + return variables[*field].textValue; + // return variables.at(variables_fields.at(varName)).textValue; } void JSONSettings::read_config_json() @@ -441,4 +460,4 @@ void JSONSettings::write_settings_json() spiffs.unmount_spiffs(); } -} +} From 4555f7fcdc28c0984257c25eebce92b54a44ad9b Mon Sep 17 00:00:00 2001 From: samamou Date: Thu, 12 Dec 2024 12:15:19 -0500 Subject: [PATCH 2/2] Add error message --- src/puara_spiffs.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/puara_spiffs.cpp b/src/puara_spiffs.cpp index b070dfd..c86b0a1 100644 --- a/src/puara_spiffs.cpp +++ b/src/puara_spiffs.cpp @@ -87,12 +87,12 @@ void SPIFFS::unmount_spiffs() //// CONFIG //// -// Can be improved double JSONSettings::getVarNumber(std::string varName) { auto field = variables_fields.find(varName); if(field == variables_fields.end()) { + std::cerr << "Error: Variable '" << varName << "' not found." << std::endl; return 0; } if(*field >= variables.size() || *field < 0) @@ -107,6 +107,7 @@ std::string JSONSettings::getVarText(std::string varName) auto field = variables_fields.find(varName); if(field == variables_fields.end()) { + std::cerr << "Error: Variable '" << varName << "' not found." << std::endl; return ""; } if(*field >= variables.size() || *field < 0) @@ -114,7 +115,6 @@ std::string JSONSettings::getVarText(std::string varName) return ""; } return variables[*field].textValue; - // return variables.at(variables_fields.at(varName)).textValue; } void JSONSettings::read_config_json()