Exception handling for json_utility APIs

This commit adds code to handle exceptions for some of the json_utility
APIs. Each API will have output returned in case of error based on the
return type of the API instead of throwing an exception.

Also updated the caller of the json_utility APIs to handle the return
value.

Change-Id: Ib43028974b61c18a0edae96608a8764cb440234a
Signed-off-by: RekhaAparna01 <vrekhaaparna@ibm.com>
diff --git a/vpd-manager/src/backup_restore.cpp b/vpd-manager/src/backup_restore.cpp
index 294efc4..9de43b9 100644
--- a/vpd-manager/src/backup_restore.cpp
+++ b/vpd-manager/src/backup_restore.cpp
@@ -20,17 +20,14 @@
 {
     std::string l_backupAndRestoreCfgFilePath =
         i_sysCfgJsonObj.value("backupRestoreConfigPath", "");
-    try
+
+    m_backupAndRestoreCfgJsonObj =
+        jsonUtility::getParsedJson(l_backupAndRestoreCfgFilePath);
+
+    if (m_backupAndRestoreCfgJsonObj.empty())
     {
-        m_backupAndRestoreCfgJsonObj =
-            jsonUtility::getParsedJson(l_backupAndRestoreCfgFilePath);
-    }
-    catch (const std::exception& ex)
-    {
-        logging::logMessage(
-            "Failed to intialize backup and restore object for file = " +
-            l_backupAndRestoreCfgFilePath);
-        throw(ex);
+        throw JsonException("JSON parsing failed",
+                            l_backupAndRestoreCfgFilePath);
     }
 }
 
diff --git a/vpd-manager/src/worker.cpp b/vpd-manager/src/worker.cpp
index 262e3dc..4199c15 100644
--- a/vpd-manager/src/worker.cpp
+++ b/vpd-manager/src/worker.cpp
@@ -496,11 +496,9 @@
         }
 
         // re-parse the JSON once appropriate JSON has been selected.
-        try
-        {
-            m_parsedJson = jsonUtility::getParsedJson(systemJson);
-        }
-        catch (const nlohmann::json::parse_error& ex)
+        m_parsedJson = jsonUtility::getParsedJson(systemJson);
+
+        if (m_parsedJson.empty())
         {
             throw(JsonException("Json parsing failed", systemJson));
         }
@@ -1459,22 +1457,25 @@
 
         // Set CollectionStatus as InProgress. Since it's an intermediate state
         // D-bus set-property call is good enough to update the status.
-        try
-        {
-            l_inventoryPath = jsonUtility::getInventoryObjPathFromJson(
-                m_parsedJson, i_vpdFilePath);
+        l_inventoryPath = jsonUtility::getInventoryObjPathFromJson(
+            m_parsedJson, i_vpdFilePath);
 
-            dbusUtility::writeDbusProperty(
-                jsonUtility::getServiceName(m_parsedJson, l_inventoryPath),
-                l_inventoryPath, constants::vpdCollectionInterface,
-                "CollectionStatus",
-                types::DbusVariantType{constants::vpdCollectionInProgress});
-        }
-        catch (const std::exception& l_exception)
+        if (!l_inventoryPath.empty())
         {
-            logging::logMessage(
-                "Unable to set CollectionStatus as InProgress for " +
-                i_vpdFilePath + ". Error : " + l_exception.what());
+            try
+            {
+                dbusUtility::writeDbusProperty(
+                    jsonUtility::getServiceName(m_parsedJson, l_inventoryPath),
+                    l_inventoryPath, constants::vpdCollectionInterface,
+                    "CollectionStatus",
+                    types::DbusVariantType{constants::vpdCollectionInProgress});
+            }
+            catch (const std::exception& l_exception)
+            {
+                logging::logMessage(
+                    "Unable to set CollectionStatus as InProgress for " +
+                    i_vpdFilePath + ". Error : " + l_exception.what());
+            }
         }
 
         const types::VPDMapVariant& parsedVpdMap = parseVpdFile(i_vpdFilePath);
@@ -1511,24 +1512,14 @@
             // logging error for these cases.
             if (vpdSpecificUtility::isPass1Planar())
             {
-                // Till exceptions are removed from utility method, this needs
-                // to be handled in place.
-                try
-                {
-                    const std::string& l_invPathLeafValue =
-                        sdbusplus::message::object_path(
-                            jsonUtility::getInventoryObjPathFromJson(
-                                m_parsedJson, i_vpdFilePath))
-                            .filename();
+                const std::string& l_invPathLeafValue =
+                    sdbusplus::message::object_path(
+                        jsonUtility::getInventoryObjPathFromJson(m_parsedJson,
+                                                                 i_vpdFilePath))
+                        .filename();
 
-                    if ((l_invPathLeafValue.find("pcie_card", 0) !=
-                         std::string::npos))
-                    {
-                        // skip logging any PEL for PCIe cards on pass 1 planar.
-                        return std::make_tuple(false, i_vpdFilePath);
-                    }
-                }
-                catch (const std::exception& l_ex)
+                if ((l_invPathLeafValue.find("pcie_card", 0) !=
+                     std::string::npos))
                 {
                     // skip logging any PEL for PCIe cards on pass 1 planar.
                     return std::make_tuple(false, i_vpdFilePath);
@@ -1665,6 +1656,12 @@
         nlohmann::json l_backupAndRestoreCfgJsonObj =
             jsonUtility::getParsedJson(l_backupAndRestoreCfgFilePath);
 
+        if (l_backupAndRestoreCfgJsonObj.empty())
+        {
+            throw JsonException("JSON parsing failed",
+                                l_backupAndRestoreCfgFilePath);
+        }
+
         // check if either of "source" or "destination" has inventory path.
         // this indicates that this sytem has System VPD on hardware
         // and other copy on D-Bus (BMC cache).