Exception handling for json_utility APIs
This commit refactors some of the json_utility APIs to handle any
exceptions thrown by it locally and only return success or failure from
the API.
Change-Id: If1d253367ad730e7f3049e8aa5d445d12ee0a057
Signed-off-by: RekhaAparna01 <vrekhaaparna@ibm.com>
diff --git a/vpd-manager/include/utility/json_utility.hpp b/vpd-manager/include/utility/json_utility.hpp
index 05e3fda..e570f9e 100644
--- a/vpd-manager/include/utility/json_utility.hpp
+++ b/vpd-manager/include/utility/json_utility.hpp
@@ -210,39 +210,44 @@
const std::string& i_vpdFilePath,
const std::string& i_flagToProcess)
{
- if (i_parsedConfigJson.empty() || i_vpdFilePath.empty() ||
- i_flagToProcess.empty())
+ try
{
- logging::logMessage(
- "Invalid parameters. Abort processing for post fail action");
-
- return false;
- }
-
- if (!(i_parsedConfigJson["frus"][i_vpdFilePath].at(0))["postFailAction"]
- .contains(i_flagToProcess))
- {
- logging::logMessage(
- "Config JSON missing flag " + i_flagToProcess +
- " to execute post fail action for path = " + i_vpdFilePath);
-
- return false;
- }
-
- for (const auto& l_tags : (i_parsedConfigJson["frus"][i_vpdFilePath].at(
- 0))["postFailAction"][i_flagToProcess]
- .items())
- {
- auto itrToFunction = funcionMap.find(l_tags.key());
- if (itrToFunction != funcionMap.end())
+ if (i_parsedConfigJson.empty() || i_vpdFilePath.empty() ||
+ i_flagToProcess.empty())
{
- if (!itrToFunction->second(i_parsedConfigJson, i_vpdFilePath,
- "postFailAction", i_flagToProcess))
+ throw std::runtime_error(
+ "Invalid parameters. Abort processing for post fail action");
+ }
+
+ if (!(i_parsedConfigJson["frus"][i_vpdFilePath].at(0))["postFailAction"]
+ .contains(i_flagToProcess))
+ {
+ throw std::runtime_error(
+ "Config JSON missing flag " + i_flagToProcess +
+ " to execute post fail action for path = " + i_vpdFilePath);
+ }
+
+ for (const auto& l_tags : (i_parsedConfigJson["frus"][i_vpdFilePath].at(
+ 0))["postFailAction"][i_flagToProcess]
+ .items())
+ {
+ auto itrToFunction = funcionMap.find(l_tags.key());
+ if (itrToFunction != funcionMap.end())
{
- return false;
+ if (!itrToFunction->second(i_parsedConfigJson, i_vpdFilePath,
+ "postFailAction", i_flagToProcess))
+ {
+ return false;
+ }
}
}
}
+ catch (const std::exception& l_ex)
+ {
+ logging::logMessage("Execute post fail action failed. Error : " +
+ std::string(l_ex.what()));
+ return false;
+ }
return true;
}
@@ -264,27 +269,24 @@
const nlohmann::json& i_parsedConfigJson, const std::string& i_vpdFilePath,
const std::string& i_baseAction, const std::string& i_flagToProcess)
{
- if (i_vpdFilePath.empty() || i_parsedConfigJson.empty() ||
- i_baseAction.empty() || i_flagToProcess.empty())
- {
- logging::logMessage(
- "Invalid parameter. Abort processing of processSystemCmd.");
- return false;
- }
-
- if (!((i_parsedConfigJson["frus"][i_vpdFilePath].at(
- 0)[i_baseAction][i_flagToProcess]["systemCmd"])
- .contains("cmd")))
- {
- logging::logMessage(
- "Config JSON missing required information to execute system command for EEPROM " +
- i_vpdFilePath);
-
- return false;
- }
-
try
{
+ if (i_vpdFilePath.empty() || i_parsedConfigJson.empty() ||
+ i_baseAction.empty() || i_flagToProcess.empty())
+ {
+ throw std::runtime_error(
+ "Invalid parameter. Abort processing of processSystemCmd.");
+ }
+
+ if (!((i_parsedConfigJson["frus"][i_vpdFilePath].at(
+ 0)[i_baseAction][i_flagToProcess]["systemCmd"])
+ .contains("cmd")))
+ {
+ throw std::runtime_error(
+ "Config JSON missing required information to execute system command for EEPROM " +
+ i_vpdFilePath);
+ }
+
const std::string& l_systemCommand =
i_parsedConfigJson["frus"][i_vpdFilePath].at(
0)[i_baseAction][i_flagToProcess]["systemCmd"]["cmd"];
@@ -292,12 +294,10 @@
commonUtility::executeCmd(l_systemCommand);
return true;
}
- catch (const std::exception& e)
+ catch (const std::exception& l_ex)
{
- std::string l_errMsg = "Process system tag failed for exception: ";
- l_errMsg += e.what();
-
- logging::logMessage(l_errMsg);
+ logging::logMessage("Process system command tag failed. Error : " +
+ std::string(l_ex.what()));
return false;
}
}