Update error handling on sdbusplus calls

Error handling was added to the bus.call() routine,
which effectively deprecated the is_method_error()
check. Errors should now be handled in a try/catch
block.

Change-Id: I1dcbbaa91db443445ef6bb2f616bd37f06731d36
Signed-off-by: Anthony Wilson <wilsonan@us.ibm.com>
diff --git a/host_state_manager.cpp b/host_state_manager.cpp
index aaac46c..7d661dd 100644
--- a/host_state_manager.cpp
+++ b/host_state_manager.cpp
@@ -8,6 +8,7 @@
 #include <cereal/types/tuple.hpp>
 #include <cereal/archives/json.hpp>
 #include <fstream>
+#include <sdbusplus/exception.hpp>
 #include <sdbusplus/server.hpp>
 #include <phosphor-logging/log.hpp>
 #include <phosphor-logging/elog-errors.hpp>
@@ -34,6 +35,7 @@
     sdbusplus::xyz::openbmc_project::State::OperatingSystem::server;
 using namespace phosphor::logging;
 namespace fs = std::experimental::filesystem;
+using sdbusplus::exception::SdBusError;
 
 // host-shutdown notifies host of shutdown and that leads to host-stop being
 // called so initiate a host shutdown with the -shutdown target and consider the
@@ -133,35 +135,37 @@
                                             SYSTEMD_INTERFACE, "GetUnit");
 
     method.append(target);
-    auto result = this->bus.call(method);
 
-    // Check that the bus call didn't result in an error
-    if (result.is_method_error())
+    try
     {
-        log<level::ERR>("Error in bus call - could not resolve GetUnit for:",
-                        entry(" %s", SYSTEMD_INTERFACE));
+        auto result = this->bus.call(method);
+        result.read(unitTargetPath);
+    }
+    catch (const SdBusError& e)
+    {
+        log<level::ERR>("Error in GetUnit call", entry("ERROR=%s", e.what()));
         return false;
     }
 
-    result.read(unitTargetPath);
-
     method = this->bus.new_method_call(
         SYSTEMD_SERVICE,
         static_cast<const std::string&>(unitTargetPath).c_str(),
         SYSTEMD_PROPERTY_IFACE, "Get");
 
     method.append(SYSTEMD_INTERFACE_UNIT, "ActiveState");
-    result = this->bus.call(method);
 
-    // Check that the bus call didn't result in an error
-    if (result.is_method_error())
+    try
     {
-        log<level::ERR>("Error in bus call - could not resolve Get for:",
-                        entry(" %s", SYSTEMD_PROPERTY_IFACE));
+        auto result = this->bus.call(method);
+        result.read(currentState);
+    }
+    catch (const SdBusError& e)
+    {
+        log<level::ERR>("Error in ActiveState Get",
+                        entry("ERROR=%s", e.what()));
         return false;
     }
 
-    result.read(currentState);
     const auto& currentStateStr =
         sdbusplus::message::variant_ns::get<std::string>(currentState);
     return currentStateStr == ACTIVE_STATE ||
@@ -176,44 +180,48 @@
         settings.service(settings.autoReboot, autoRebootIntf).c_str(),
         settings.autoReboot.c_str(), "org.freedesktop.DBus.Properties", "Get");
     method.append(autoRebootIntf, "AutoReboot");
-    auto reply = bus.call(method);
-    if (reply.is_method_error())
-    {
-        log<level::ERR>("Error in AutoReboot Get");
-        return false;
-    }
 
-    sdbusplus::message::variant<bool> result;
-    reply.read(result);
-    auto autoReboot = sdbusplus::message::variant_ns::get<bool>(result);
-    auto rebootCounterParam = reboot::RebootAttempts::attemptsLeft();
-
-    if (autoReboot)
+    try
     {
-        if (rebootCounterParam > 0)
+        auto reply = bus.call(method);
+
+        sdbusplus::message::variant<bool> result;
+        reply.read(result);
+        auto autoReboot = sdbusplus::message::variant_ns::get<bool>(result);
+        auto rebootCounterParam = reboot::RebootAttempts::attemptsLeft();
+
+        if (autoReboot)
         {
-            // Reduce BOOTCOUNT by 1
-            log<level::INFO>("Auto reboot enabled, rebooting");
-            return true;
-        }
-        else if (rebootCounterParam == 0)
-        {
-            // Reset reboot counter and go to quiesce state
-            log<level::INFO>("Auto reboot enabled. "
-                             "HOST BOOTCOUNT already set to 0.");
-            attemptsLeft(BOOT_COUNT_MAX_ALLOWED);
-            return false;
+            if (rebootCounterParam > 0)
+            {
+                // Reduce BOOTCOUNT by 1
+                log<level::INFO>("Auto reboot enabled, rebooting");
+                return true;
+            }
+            else if (rebootCounterParam == 0)
+            {
+                // Reset reboot counter and go to quiesce state
+                log<level::INFO>("Auto reboot enabled. "
+                                 "HOST BOOTCOUNT already set to 0.");
+                attemptsLeft(BOOT_COUNT_MAX_ALLOWED);
+                return false;
+            }
+            else
+            {
+                log<level::INFO>("Auto reboot enabled. "
+                                 "HOST BOOTCOUNT has an invalid value.");
+                return false;
+            }
         }
         else
         {
-            log<level::INFO>("Auto reboot enabled. "
-                             "HOST BOOTCOUNT has an invalid value.");
+            log<level::INFO>("Auto reboot disabled.");
             return false;
         }
     }
-    else
+    catch (const SdBusError& e)
     {
-        log<level::INFO>("Auto reboot disabled.");
+        log<level::ERR>("Error in AutoReboot Get", entry("ERROR=%s", e.what()));
         return false;
     }
 }