Set error code for isActionRequired API

This commit updates isActionRequired API to set error code in case of
error. This helps caller of API to take action based on the error code
returned from the API.

Change-Id: Ib706a3491b502ea9016bf327a6487e98a96c1ea8
Signed-off-by: Rekha Aparna <vrekhaaparna@ibm.com>
diff --git a/vpd-manager/include/utility/json_utility.hpp b/vpd-manager/include/utility/json_utility.hpp
index ea92137..9217598 100644
--- a/vpd-manager/include/utility/json_utility.hpp
+++ b/vpd-manager/include/utility/json_utility.hpp
@@ -717,28 +717,29 @@
  * @param[in] i_action - Action to be checked.
  * @param[in] i_flowFlag - Denotes the flow w.r.t which the action should be
  * triggered.
+ * @param[out] o_errCode - To set error code in case of error.
  * @return - True if action is defined for the flow, false otherwise.
  */
-inline bool isActionRequired(
-    const nlohmann::json& i_sysCfgJsonObj, const std::string& i_vpdFruPath,
-    const std::string& i_action, const std::string& i_flowFlag)
+inline bool isActionRequired(const nlohmann::json& i_sysCfgJsonObj,
+                             const std::string& i_vpdFruPath,
+                             const std::string& i_action,
+                             const std::string& i_flowFlag, uint16_t& o_errCode)
 {
     if (i_vpdFruPath.empty() || i_action.empty() || i_flowFlag.empty())
     {
-        logging::logMessage("Invalid parameters recieved.");
+        o_errCode = error_code::INVALID_INPUT_PARAMETER;
         return false;
     }
 
     if (!i_sysCfgJsonObj.contains("frus"))
     {
-        logging::logMessage("Invalid JSON object recieved.");
+        o_errCode = error_code::INVALID_JSON;
         return false;
     }
 
     if (!i_sysCfgJsonObj["frus"].contains(i_vpdFruPath))
     {
-        logging::logMessage(
-            "JSON object does not contain EEPROM path " + i_vpdFruPath);
+        o_errCode = error_code::FRU_PATH_NOT_FOUND;
         return false;
     }
 
@@ -749,11 +750,6 @@
         {
             return true;
         }
-
-        logging::logMessage("Flow flag: [" + i_flowFlag +
-                            "], not found in JSON for path: " + i_vpdFruPath +
-                            " while checking for action: " + i_action);
-        return false;
     }
     return false;
 }
@@ -790,10 +786,24 @@
 
         for (const auto& l_fru : i_sysCfgJsonObj["frus"].items())
         {
+            uint16_t l_errCode = 0;
             const auto l_fruPath = l_fru.key();
 
-            if (isActionRequired(i_sysCfgJsonObj, l_fruPath, "pollingRequired",
-                                 "hotPlugging"))
+            bool l_isHotPluggableFru =
+                isActionRequired(i_sysCfgJsonObj, l_fruPath, "pollingRequired",
+                                 "hotPlugging", l_errCode);
+
+            if (l_errCode)
+            {
+                logging::logMessage(
+                    "Error while checking if action required for FRU [" +
+                    std::string(l_fruPath) + "], error : " +
+                    vpdSpecificUtility::getErrCodeMsg(l_errCode));
+
+                return l_gpioPollingRequiredFrusList;
+            }
+
+            if (l_isHotPluggableFru)
             {
                 if (i_sysCfgJsonObj["frus"][l_fruPath]
                         .at(0)["pollingRequired"]["hotPlugging"]
diff --git a/vpd-manager/src/worker.cpp b/vpd-manager/src/worker.cpp
index b725745..7a4ce00 100644
--- a/vpd-manager/src/worker.cpp
+++ b/vpd-manager/src/worker.cpp
@@ -1284,6 +1284,8 @@
 {
     try
     {
+        uint16_t l_errCode = 0;
+
         if (i_vpdFilePath.empty())
         {
             throw std::runtime_error(
@@ -1293,10 +1295,10 @@
 
         bool isPreActionRequired = false;
         if (jsonUtility::isActionRequired(m_parsedJson, i_vpdFilePath,
-                                          "preAction", "collection"))
+                                          "preAction", "collection", l_errCode))
         {
+            l_errCode = 0;
             isPreActionRequired = true;
-            uint16_t l_errCode = 0;
             if (!processPreAction(i_vpdFilePath, "collection", l_errCode))
             {
                 if (l_errCode == error_code::DEVICE_NOT_PRESENT)
@@ -1315,6 +1317,13 @@
                     vpdSpecificUtility::getErrCodeMsg(l_errCode));
             }
         }
+        else if (l_errCode)
+        {
+            logging::logMessage(
+                "Failed to check if pre action required for FRU [" +
+                i_vpdFilePath +
+                "], error : " + vpdSpecificUtility::getErrCodeMsg(l_errCode));
+        }
 
         if (!std::filesystem::exists(i_vpdFilePath))
         {
@@ -1336,8 +1345,11 @@
         // any post action in the flow of collection.
         // Note: Don't change the order, post action needs to be processed only
         // after collection for FRU is successfully done.
+        l_errCode = 0;
+
         if (jsonUtility::isActionRequired(m_parsedJson, i_vpdFilePath,
-                                          "postAction", "collection"))
+                                          "postAction", "collection",
+                                          l_errCode))
         {
             if (!processPostAction(i_vpdFilePath, "collection", l_parsedVpd))
             {
@@ -1351,6 +1363,13 @@
                     std::nullopt, std::nullopt, std::nullopt, std::nullopt);
             }
         }
+        else if (l_errCode)
+        {
+            logging::logMessage(
+                "Error while checking if post action required for FRU [" +
+                i_vpdFilePath +
+                "], error : " + vpdSpecificUtility::getErrCodeMsg(l_errCode));
+        }
 
         return l_parsedVpd;
     }
@@ -1363,7 +1382,8 @@
 
         // If post fail action is required, execute it.
         if (jsonUtility::isActionRequired(m_parsedJson, i_vpdFilePath,
-                                          "postFailAction", "collection"))
+                                          "postFailAction", "collection",
+                                          l_errCode))
         {
             if (!jsonUtility::executePostFailAction(m_parsedJson, i_vpdFilePath,
                                                     "collection", l_errCode))
@@ -1373,6 +1393,12 @@
                            " Aborting collection for this FRU.";
             }
         }
+        else if (l_errCode)
+        {
+            l_exMsg +=
+                ". Failed to check if post fail action required, error : " +
+                vpdSpecificUtility::getErrCodeMsg(l_errCode);
+        }
 
         if (typeid(l_ex) == typeid(DataException))
         {
@@ -1702,9 +1728,9 @@
             else
             {
                 if (jsonUtility::isActionRequired(m_parsedJson, l_fruPath,
-                                                  "preAction", "deletion"))
+                                                  "preAction", "deletion",
+                                                  l_errCode))
                 {
-                    uint16_t l_errCode = 0;
                     if (!processPreAction(l_fruPath, "deletion", l_errCode))
                     {
                         std::string l_msg = "Pre action failed";
@@ -1717,6 +1743,13 @@
                         throw std::runtime_error(l_msg);
                     }
                 }
+                else if (l_errCode)
+                {
+                    logging::logMessage(
+                        "Failed to check if pre action required for FRU [" +
+                        l_fruPath + "], error : " +
+                        vpdSpecificUtility::getErrCodeMsg(l_errCode));
+                }
 
                 std::vector<std::string> l_interfaceList{
                     constants::operationalStatusInf};
@@ -1750,14 +1783,24 @@
                     throw std::runtime_error("Call to PIM failed.");
                 }
 
+                l_errCode = 0;
+
                 if (jsonUtility::isActionRequired(m_parsedJson, l_fruPath,
-                                                  "postAction", "deletion"))
+                                                  "postAction", "deletion",
+                                                  l_errCode))
                 {
                     if (!processPostAction(l_fruPath, "deletion"))
                     {
                         throw std::runtime_error("Post action failed");
                     }
                 }
+                else if (l_errCode)
+                {
+                    logging::logMessage(
+                        "Failed to check if post action required during deletion for FRU [" +
+                        l_fruPath + "], error : " +
+                        vpdSpecificUtility::getErrCodeMsg(l_errCode));
+                }
             }
         }
         else
@@ -1779,7 +1822,8 @@
             " error: " + std::string(l_ex.what());
 
         if (jsonUtility::isActionRequired(m_parsedJson, l_fruPath,
-                                          "postFailAction", "deletion"))
+                                          "postFailAction", "deletion",
+                                          l_errCode))
         {
             if (!jsonUtility::executePostFailAction(m_parsedJson, l_fruPath,
                                                     "deletion", l_errCode))
@@ -1788,6 +1832,12 @@
                             vpdSpecificUtility::getErrCodeMsg(l_errCode);
             }
         }
+        else if (l_errCode)
+        {
+            l_errMsg +=
+                ". Failed to check if post fail action required, error : " +
+                vpdSpecificUtility::getErrCodeMsg(l_errCode);
+        }
 
         logging::logMessage(l_errMsg);
     }