Update json_utility APIs with error codes

This commit updates API getVPDOffset and getInventoryObjPathFromJson to
set error codes in case of errors/exceptions. This helps caller of the
APIs to take action based on the error code returned from the API.

Change-Id: Icfe2640ee311d61e83ca302e5199ce4dd1057c8b
Signed-off-by: Rekha Aparna <vrekhaaparna@ibm.com>
diff --git a/vpd-manager/src/backup_restore.cpp b/vpd-manager/src/backup_restore.cpp
index 6c31eac..61d5ba5 100644
--- a/vpd-manager/src/backup_restore.cpp
+++ b/vpd-manager/src/backup_restore.cpp
@@ -162,14 +162,38 @@
         return;
     }
 
-    const std::string l_srcInvPath =
-        jsonUtility::getInventoryObjPathFromJson(m_sysCfgJsonObj, i_srcPath);
-    const std::string l_dstInvPath =
-        jsonUtility::getInventoryObjPathFromJson(m_sysCfgJsonObj, i_dstPath);
-    if (l_srcInvPath.empty() || l_dstInvPath.empty())
+    uint16_t l_errCode = 0;
+    const std::string l_srcInvPath = jsonUtility::getInventoryObjPathFromJson(
+        m_sysCfgJsonObj, i_srcPath, l_errCode);
+
+    if (l_srcInvPath.empty())
     {
-        logging::logMessage(
-            "Couldn't find either source or destination inventory path.");
+        if (l_errCode)
+        {
+            logging::logMessage(
+                "Couldn't find source inventory path. Error : " +
+                vpdSpecificUtility::getErrCodeMsg(l_errCode));
+            return;
+        }
+
+        logging::logMessage("Couldn't find  source inventory path.");
+        return;
+    }
+
+    const std::string l_dstInvPath = jsonUtility::getInventoryObjPathFromJson(
+        m_sysCfgJsonObj, i_dstPath, l_errCode);
+
+    if (l_dstInvPath.empty())
+    {
+        if (l_errCode)
+        {
+            logging::logMessage(
+                "Couldn't find destination inventory path. Error : " +
+                vpdSpecificUtility::getErrCodeMsg(l_errCode));
+            return;
+        }
+
+        logging::logMessage("Couldn't find destination inventory path.");
         return;
     }
 
diff --git a/vpd-manager/src/event_logger.cpp b/vpd-manager/src/event_logger.cpp
index 900361a..a9ba077 100644
--- a/vpd-manager/src/event_logger.cpp
+++ b/vpd-manager/src/event_logger.cpp
@@ -8,6 +8,7 @@
 #include <systemd/sd-bus.h>
 
 #include <utility/json_utility.hpp>
+#include <utility/vpd_specific_utility.hpp>
 
 #include <filesystem>
 
@@ -339,6 +340,8 @@
         // Path to hold callout inventory path.
         std::string l_calloutInvPath;
 
+        uint16_t l_errCode = 0;
+
         // check if callout path is a valid inventory path. if not, get the JSON
         // object to get inventory path.
         if (std::get<0>(i_callouts[0])
@@ -353,7 +356,7 @@
                 {
                     l_calloutInvPath = jsonUtility::getInventoryObjPathFromJson(
                         jsonUtility::getParsedJson(INVENTORY_JSON_SYM_LINK),
-                        std::get<0>(i_callouts[0]));
+                        std::get<0>(i_callouts[0]), l_errCode);
                 }
                 else
                 {
@@ -366,6 +369,14 @@
         if (l_calloutInvPath.empty())
         {
             l_calloutInvPath = std::get<0>(i_callouts[0]);
+
+            if (l_errCode)
+            {
+                logging::logMessage(
+                    "Failed to get inventory object path from JSON for FRU [" +
+                    std::get<0>(i_callouts[0]) + "], error : " +
+                    vpdSpecificUtility::getErrCodeMsg(l_errCode));
+            }
         }
 
         const std::map<std::string, std::string> l_additionalData{
diff --git a/vpd-manager/src/gpio_monitor.cpp b/vpd-manager/src/gpio_monitor.cpp
index 2b02c9d..d758c90 100644
--- a/vpd-manager/src/gpio_monitor.cpp
+++ b/vpd-manager/src/gpio_monitor.cpp
@@ -1,10 +1,12 @@
 #include "gpio_monitor.hpp"
 
 #include "constants.hpp"
+#include "error_codes.hpp"
 #include "logger.hpp"
 #include "types.hpp"
 #include "utility/dbus_utility.hpp"
 #include "utility/json_utility.hpp"
+#include "utility/vpd_specific_utility.hpp"
 
 #include <boost/asio.hpp>
 #include <boost/bind/bind.hpp>
@@ -42,8 +44,18 @@
         }
         else
         {
-            m_worker->deleteFruVpd(jsonUtility::getInventoryObjPathFromJson(
-                m_worker->getSysCfgJsonObj(), m_fruPath));
+            uint16_t l_errCode = 0;
+            std::string l_invPath = jsonUtility::getInventoryObjPathFromJson(
+                m_worker->getSysCfgJsonObj(), m_fruPath, l_errCode);
+
+            if (l_errCode)
+            {
+                throw std::runtime_error(
+                    "Failed to get inventory path from JSON, error : " +
+                    vpdSpecificUtility::getErrCodeMsg(l_errCode));
+            }
+
+            m_worker->deleteFruVpd(l_invPath);
         }
     }
     catch (std::exception& l_ex)
diff --git a/vpd-manager/src/parser.cpp b/vpd-manager/src/parser.cpp
index 7658011..a32d155 100644
--- a/vpd-manager/src/parser.cpp
+++ b/vpd-manager/src/parser.cpp
@@ -34,7 +34,17 @@
     // Read VPD offset if applicable.
     if (!m_parsedJson.empty())
     {
-        m_vpdStartOffset = jsonUtility::getVPDOffset(m_parsedJson, vpdFilePath);
+        uint16_t l_errorCode = 0;
+
+        m_vpdStartOffset =
+            jsonUtility::getVPDOffset(m_parsedJson, vpdFilePath, l_errorCode);
+
+        if (l_errorCode)
+        {
+            logging::logMessage(
+                "Failed to get vpd offset for path [" + m_vpdFilePath +
+                "], error: " + vpdSpecificUtility::getErrCodeMsg(l_errorCode));
+        }
     }
 }
 
diff --git a/vpd-manager/src/worker.cpp b/vpd-manager/src/worker.cpp
index 179e7ec..5f8d888 100644
--- a/vpd-manager/src/worker.cpp
+++ b/vpd-manager/src/worker.cpp
@@ -1369,10 +1369,12 @@
         m_activeCollectionThreadCount++;
         m_mutex.unlock();
 
-        // Set collection Status as InProgress. Since it's an intermediate state
+        uint16_t l_errCode = 0;
+
+        // Set CollectionStatus as InProgress. Since it's an intermediate state
         // D-bus set-property call is good enough to update the status.
         l_inventoryPath = jsonUtility::getInventoryObjPathFromJson(
-            m_parsedJson, i_vpdFilePath);
+            m_parsedJson, i_vpdFilePath, l_errCode);
 
         if (!l_inventoryPath.empty())
         {
@@ -1391,6 +1393,12 @@
                     i_vpdFilePath + ". Error : " + "DBus write failed");
             }
         }
+        else if (l_errCode)
+        {
+            logging::logMessage(
+                "Failed to get inventory path for FRU [" + i_vpdFilePath +
+                "], error : " + vpdSpecificUtility::getErrCodeMsg(l_errCode));
+        }
 
         const types::VPDMapVariant& parsedVpdMap = parseVpdFile(i_vpdFilePath);
         if (!std::holds_alternative<std::monostate>(parsedVpdMap))
@@ -1429,15 +1437,25 @@
         // based on status of execution.
         if (typeid(ex) == std::type_index(typeid(DataException)))
         {
+            uint16_t l_errCode = 0;
             // In case of pass1 planar, VPD can be corrupted on PCIe cards. Skip
             // logging error for these cases.
             if (vpdSpecificUtility::isPass1Planar())
             {
+                std::string l_invPath =
+                    jsonUtility::getInventoryObjPathFromJson(
+                        m_parsedJson, i_vpdFilePath, l_errCode);
+
+                if (l_errCode != 0)
+                {
+                    logging::logMessage(
+                        "Failed to get inventory object path from JSON for FRU [" +
+                        i_vpdFilePath + "], error: " +
+                        vpdSpecificUtility::getErrCodeMsg(l_errCode));
+                }
+
                 const std::string& l_invPathLeafValue =
-                    sdbusplus::message::object_path(
-                        jsonUtility::getInventoryObjPathFromJson(m_parsedJson,
-                                                                 i_vpdFilePath))
-                        .filename();
+                    sdbusplus::message::object_path(l_invPath).filename();
 
                 if ((l_invPathLeafValue.find("pcie_card", 0) !=
                      std::string::npos))
@@ -1499,11 +1517,22 @@
             return true;
         }
 
+        uint16_t l_errCode = 0;
+        std::string l_invPath = jsonUtility::getInventoryObjPathFromJson(
+            m_parsedJson, i_vpdFilePath, l_errCode);
+
+        if (l_errCode)
+        {
+            logging::logMessage(
+                "Failed to get inventory path from JSON for FRU [" +
+                i_vpdFilePath +
+                "], error : " + vpdSpecificUtility::getErrCodeMsg(l_errCode));
+
+            return false;
+        }
+
         const std::string& l_invPathLeafValue =
-            sdbusplus::message::object_path(
-                jsonUtility::getInventoryObjPathFromJson(m_parsedJson,
-                                                         i_vpdFilePath))
-                .filename();
+            sdbusplus::message::object_path(l_invPath).filename();
 
         if ((l_invPathLeafValue.find("pcie_card", 0) != std::string::npos))
         {