Refactor exception handling for writeDbusProperty API

This commit refactors dbus_utility API used to update property value on
DBus to handle any exceptions thrown by it locally and return only
success or failure as the return type.

Also updated the caller of this API to handle the return value.

Change-Id: Ia92c1b0928d02cc0130a8b4e547ba3c6069d0d2f
Signed-off-by: RekhaAparna01 <vrekhaaparna@ibm.com>
diff --git a/vpd-manager/include/utility/dbus_utility.hpp b/vpd-manager/include/utility/dbus_utility.hpp
index f849f89..d13e123 100644
--- a/vpd-manager/include/utility/dbus_utility.hpp
+++ b/vpd-manager/include/utility/dbus_utility.hpp
@@ -214,46 +214,41 @@
  * identify any write failure. The API in no other way indicate write  failure
  * to the caller.
  *
- * Note: It will be caller's responsibility ho handle the exception thrown in
- * case of write failure and generate appropriate error.
- *
  * @param [in] serviceName - Name of the Dbus service.
  * @param [in] objectPath - Object path under the service.
  * @param [in] interface - Interface under which property exist.
  * @param [in] property - Property whose value is to be written.
  * @param [in] propertyValue - The value to be written.
+ * @return True if write on DBus is success, false otherwise.
  */
-inline void writeDbusProperty(
+inline bool writeDbusProperty(
     const std::string& serviceName, const std::string& objectPath,
     const std::string& interface, const std::string& property,
     const types::DbusVariantType& propertyValue)
 {
-    // Mandatory fields to make a write dbus call.
-    if (serviceName.empty() || objectPath.empty() || interface.empty() ||
-        property.empty())
-    {
-        logging::logMessage(
-            "One of the parameter to make Dbus read call is empty.");
-
-        // caller need to handle the throw to ensure Dbus write success.
-        throw std::runtime_error("Dbus write failed, Parameter empty");
-    }
-
     try
     {
+        // Mandatory fields to make a write dbus call.
+        if (serviceName.empty() || objectPath.empty() || interface.empty() ||
+            property.empty())
+        {
+            throw std::runtime_error("Dbus write failed, Parameter empty");
+        }
+
         auto bus = sdbusplus::bus::new_default();
         auto method =
             bus.new_method_call(serviceName.c_str(), objectPath.c_str(),
                                 "org.freedesktop.DBus.Properties", "Set");
         method.append(interface, property, propertyValue);
         bus.call(method);
-    }
-    catch (const sdbusplus::exception::SdBusError& e)
-    {
-        logging::logMessage(e.what());
 
-        // caller needs to handle this throw to handle error in writing Dbus.
-        throw std::runtime_error("Dbus write failed");
+        return true;
+    }
+    catch (const std::exception& l_ex)
+    {
+        logging::logMessage(
+            "DBus write failed, error: " + std::string(l_ex.what()));
+        return false;
     }
 }
 
diff --git a/vpd-manager/src/bios_handler.cpp b/vpd-manager/src/bios_handler.cpp
index 59094e0..65e0c47 100644
--- a/vpd-manager/src/bios_handler.cpp
+++ b/vpd-manager/src/bios_handler.cpp
@@ -290,19 +290,14 @@
             "xyz.openbmc_project.BIOSConfig.Manager.AttributeType.Integer",
             i_fcoVal.at(constants::VALUE_3))));
 
-    try
-    {
-        dbusUtility::writeDbusProperty(
+    if (!dbusUtility::writeDbusProperty(
             constants::biosConfigMgrService, constants::biosConfigMgrObjPath,
             constants::biosConfigMgrInterface, "PendingAttributes",
-            l_pendingBiosAttribute);
-    }
-    catch (const std::exception& l_ex)
+            l_pendingBiosAttribute))
     {
         // TODO: Should we log informational PEL here as well?
         logging::logMessage(
-            "DBus call to update FCO value in pending attribute failed. " +
-            std::string(l_ex.what()));
+            "DBus call to update FCO value in pending attribute failed. ");
     }
 }
 
@@ -370,19 +365,14 @@
             "xyz.openbmc_project.BIOSConfig.Manager.AttributeType.Enumeration",
             l_valtoUpdate)));
 
-    try
-    {
-        dbusUtility::writeDbusProperty(
+    if (!dbusUtility::writeDbusProperty(
             constants::biosConfigMgrService, constants::biosConfigMgrObjPath,
             constants::biosConfigMgrInterface, "PendingAttributes",
-            l_pendingBiosAttribute);
-    }
-    catch (const std::exception& l_ex)
+            l_pendingBiosAttribute))
     {
         // TODO: Should we log informational PEL here as well?
         logging::logMessage(
-            "DBus call to update AMM value in pending attribute failed. " +
-            std::string(l_ex.what()));
+            "DBus call to update AMM value in pending attribute failed.");
     }
 }
 
@@ -505,18 +495,13 @@
             "xyz.openbmc_project.BIOSConfig.Manager.AttributeType.Enumeration",
             l_valtoUpdate)));
 
-    try
-    {
-        dbusUtility::writeDbusProperty(
+    if (!dbusUtility::writeDbusProperty(
             constants::biosConfigMgrService, constants::biosConfigMgrObjPath,
             constants::biosConfigMgrInterface, "PendingAttributes",
-            l_pendingBiosAttribute);
-    }
-    catch (const std::exception& l_ex)
+            l_pendingBiosAttribute))
     {
         logging::logMessage(
-            "DBus call to update lpar value in pending attribute failed. " +
-            std::string(l_ex.what()));
+            "DBus call to update lpar value in pending attribute failed.");
     }
 
     return;
@@ -621,18 +606,13 @@
             "xyz.openbmc_project.BIOSConfig.Manager.AttributeType.Enumeration",
             l_valtoUpdate)));
 
-    try
-    {
-        dbusUtility::writeDbusProperty(
+    if (!dbusUtility::writeDbusProperty(
             constants::biosConfigMgrService, constants::biosConfigMgrObjPath,
             constants::biosConfigMgrInterface, "PendingAttributes",
-            l_pendingBiosAttribute);
-    }
-    catch (const std::exception& l_ex)
+            l_pendingBiosAttribute))
     {
         logging::logMessage(
-            "DBus call to update NVRAM value in pending attribute failed. " +
-            std::string(l_ex.what()));
+            "DBus call to update NVRAM value in pending attribute failed.");
     }
 }
 
@@ -732,18 +712,13 @@
             "xyz.openbmc_project.BIOSConfig.Manager.AttributeType.Enumeration",
             l_valtoUpdate)));
 
-    try
-    {
-        dbusUtility::writeDbusProperty(
+    if (!dbusUtility::writeDbusProperty(
             constants::biosConfigMgrService, constants::biosConfigMgrObjPath,
             constants::biosConfigMgrInterface, "PendingAttributes",
-            l_pendingBiosAttribute);
-    }
-    catch (const std::exception& l_ex)
+            l_pendingBiosAttribute))
     {
         logging::logMessage(
-            "DBus call to update keep and clear value in pending attribute failed. " +
-            std::string(l_ex.what()));
+            "DBus call to update keep and clear value in pending attribute failed.");
     }
 }
 
diff --git a/vpd-manager/src/manager.cpp b/vpd-manager/src/manager.cpp
index 3ab6ec1..eca1588 100644
--- a/vpd-manager/src/manager.cpp
+++ b/vpd-manager/src/manager.cpp
@@ -543,17 +543,14 @@
 
         // Set CollectionStatus as InProgress. Since it's an intermediate state
         // D-bus set-property call is good enough to update the status.
-        try
-        {
-            const std::string& l_collStatusProp = "CollectionStatus";
-            dbusUtility::writeDbusProperty(
+        const std::string& l_collStatusProp = "CollectionStatus";
+
+        if (!dbusUtility::writeDbusProperty(
                 jsonUtility::getServiceName(l_sysCfgJsonObj,
                                             std::string(i_dbusObjPath)),
                 std::string(i_dbusObjPath), constants::vpdCollectionInterface,
                 l_collStatusProp,
-                types::DbusVariantType{constants::vpdCollectionInProgress});
-        }
-        catch (const std::exception& e)
+                types::DbusVariantType{constants::vpdCollectionInProgress}))
         {
             logging::logMessage(
                 "Unable to set CollectionStatus as InProgress for " +
diff --git a/vpd-manager/src/worker.cpp b/vpd-manager/src/worker.cpp
index 4199c15..1529924 100644
--- a/vpd-manager/src/worker.cpp
+++ b/vpd-manager/src/worker.cpp
@@ -1462,19 +1462,15 @@
 
         if (!l_inventoryPath.empty())
         {
-            try
-            {
-                dbusUtility::writeDbusProperty(
+            if (!dbusUtility::writeDbusProperty(
                     jsonUtility::getServiceName(m_parsedJson, l_inventoryPath),
                     l_inventoryPath, constants::vpdCollectionInterface,
                     "CollectionStatus",
-                    types::DbusVariantType{constants::vpdCollectionInProgress});
-            }
-            catch (const std::exception& l_exception)
+                    types::DbusVariantType{constants::vpdCollectionInProgress}))
             {
                 logging::logMessage(
                     "Unable to set CollectionStatus as InProgress for " +
-                    i_vpdFilePath + ". Error : " + l_exception.what());
+                    i_vpdFilePath + ". Error : " + "DBus write failed");
             }
         }