Handle exceptions from JSON utility APIs

This commit adds proper handling for exceptions thrown by JSON utility
APIs. In some cases, input parameters were being accessed directly from
the JSON object without verifying their presence, leading to potential
exceptions. This commit ensures safer access to JSON fields to prevent
such errors.

Change-Id: I0ae17b3186bf868ea5e0c2e6765dafc76d962f27
Signed-off-by: Rekha Aparna <vrekhaaparna@ibm.com>
diff --git a/vpd-manager/include/error_codes.hpp b/vpd-manager/include/error_codes.hpp
index 648215e..cea46f1 100644
--- a/vpd-manager/include/error_codes.hpp
+++ b/vpd-manager/include/error_codes.hpp
@@ -28,7 +28,8 @@
     INVALID_INPUT_PARAMETER,
     DEVICE_NOT_PRESENT,
     DEVICE_PRESENCE_UNKNOWN,
-    GPIO_LINE_EXCEPTION
+    GPIO_LINE_EXCEPTION,
+    ERROR_PROCESSING_SYSTEM_CMD
 };
 
 const std::unordered_map<int, std::string> errorCodeMap = {
@@ -55,5 +56,7 @@
     {error_code::DEVICE_NOT_PRESENT,
      "Presence pin read successfully but device was absent."},
     {error_code::DEVICE_PRESENCE_UNKNOWN, "Exception on presence line GPIO."},
-    {error_code::GPIO_LINE_EXCEPTION, "There was an exception in GPIO line."}};
+    {error_code::GPIO_LINE_EXCEPTION, "There was an exception in GPIO line."},
+    {error_code::ERROR_PROCESSING_SYSTEM_CMD,
+     "Error while executing system command tag."}};
 } // namespace vpd
diff --git a/vpd-manager/include/utility/json_utility.hpp b/vpd-manager/include/utility/json_utility.hpp
index 8de6f7c..8e7be98 100644
--- a/vpd-manager/include/utility/json_utility.hpp
+++ b/vpd-manager/include/utility/json_utility.hpp
@@ -225,6 +225,25 @@
         return false;
     }
 
+    if (!i_parsedConfigJson.contains("frus"))
+    {
+        o_errCode = error_code::INVALID_JSON;
+        return false;
+    }
+
+    if (!i_parsedConfigJson["frus"].contains(i_vpdFilePath))
+    {
+        o_errCode = error_code::FRU_PATH_NOT_FOUND;
+        return false;
+    }
+
+    if (!i_parsedConfigJson["frus"][i_vpdFilePath].at(0).contains(
+            "postFailAction"))
+    {
+        o_errCode = error_code::MISSING_ACTION_TAG;
+        return false;
+    }
+
     if (!(i_parsedConfigJson["frus"][i_vpdFilePath].at(0))["postFailAction"]
              .contains(i_flagToProcess))
     {
@@ -284,19 +303,27 @@
         return false;
     }
 
-    if (!((i_parsedConfigJson["frus"][i_vpdFilePath].at(
-               0)[i_baseAction][i_flagToProcess]["systemCmd"])
-              .contains("cmd")))
+    try
     {
-        o_errCode = error_code::MISSING_FLAG;
+        if (!((i_parsedConfigJson["frus"][i_vpdFilePath].at(
+                   0)[i_baseAction][i_flagToProcess]["systemCmd"])
+                  .contains("cmd")))
+        {
+            o_errCode = error_code::MISSING_FLAG;
+            return false;
+        }
+
+        const std::string& l_systemCommand =
+            i_parsedConfigJson["frus"][i_vpdFilePath].at(
+                0)[i_baseAction][i_flagToProcess]["systemCmd"]["cmd"];
+
+        commonUtility::executeCmd(l_systemCommand);
+    }
+    catch (const std::exception& l_ex)
+    {
+        o_errCode = error_code::ERROR_PROCESSING_SYSTEM_CMD;
         return false;
     }
-
-    const std::string& l_systemCommand =
-        i_parsedConfigJson["frus"][i_vpdFilePath].at(
-            0)[i_baseAction][i_flagToProcess]["systemCmd"]["cmd"];
-
-    commonUtility::executeCmd(l_systemCommand);
     return true;
 }
 
@@ -1040,6 +1067,12 @@
         return false;
     }
 
+    if (!i_sysCfgJsonObj["frus"].contains(i_vpdFruPath))
+    {
+        o_errCode = error_code::FRU_PATH_NOT_FOUND;
+        return false;
+    }
+
     return (
         (i_sysCfgJsonObj["frus"][i_vpdFruPath].at(0))
             .contains("replaceableAtRuntime") &&
@@ -1070,6 +1103,13 @@
     if (i_sysCfgJsonObj.empty() || (!i_sysCfgJsonObj.contains("frus")))
     {
         o_errCode = error_code::INVALID_JSON;
+        return false;
+    }
+
+    if (!i_sysCfgJsonObj["frus"].contains(i_vpdFruPath))
+    {
+        o_errCode = error_code::FRU_PATH_NOT_FOUND;
+        return false;
     }
 
     return (