chassis-state-manager: Check for dbus call errors

Making the query to the power state manager can fail if that daemon
does not come up before the chassis state manager. Since we added
exceptions to sdbusplus for call errors, we get an unhandled exception
if this daemon is not running. Since the pgood daemon will announce the
power status when it starts up, we can just assume the power is off if
the call fails. Eventually it will become correct. This restores the
same behavior we had in the past.

Tested:
    Ran on a zaius machine and it no longer fails / prints errors in the
    common cases and eventually gets to the proper state.

Change-Id: I78809d747219f1096e0ea36905199a35e3b93caa
Signed-off-by: William A. Kennington III <wak@google.com>
diff --git a/chassis_state_manager.cpp b/chassis_state_manager.cpp
index f333254..03dd176 100644
--- a/chassis_state_manager.cpp
+++ b/chassis_state_manager.cpp
@@ -67,33 +67,52 @@
         "org.freedesktop.DBus.Properties", "Get");
 
     method.append("org.openbmc.control.Power", "pgood");
-    auto reply = this->bus.call(method);
-    if (reply.is_method_error())
-    {
-        log<level::ERR>("Error in bus call - could not get initial pgood");
-        goto fail;
-    }
-
     try
     {
-        reply.read(pgood);
+        auto reply = this->bus.call(method);
+        if (reply.is_method_error())
+        {
+            log<level::ERR>(
+                "Error in response message - could not get initial pgood");
+            goto fail;
+        }
+
+        try
+        {
+            reply.read(pgood);
+        }
+        catch (const SdBusError& e)
+        {
+            log<level::ERR>("Error in bus response - bad encoding of pgood",
+                            entry("ERROR=%s", e.what()),
+                            entry("REPLY_SIG=%s", reply.get_signature()));
+            goto fail;
+        }
+
+        if (pgood == 1)
+        {
+            log<level::INFO>("Initial Chassis State will be On",
+                             entry("CHASSIS_CURRENT_POWER_STATE=%s",
+                                   convertForMessage(PowerState::On).c_str()));
+            server::Chassis::currentPowerState(PowerState::On);
+            server::Chassis::requestedPowerTransition(Transition::On);
+            return;
+        }
     }
     catch (const SdBusError& e)
     {
-        log<level::ERR>("Error in bus response - bad encoding of pgood",
-                        entry("ERROR=%s", e.what()),
-                        entry("REPLY_SIG=%s", reply.get_signature()));
-        goto fail;
-    }
+        // It's acceptable for the pgood state service to not be available
+        // since it will notify us of the pgood state when it comes up.
+        if (e.name() != nullptr &&
+            strcmp("org.freedesktop.DBus.Error.ServiceUnknown", e.name()) == 0)
+        {
+            goto fail;
+        }
 
-    if (pgood == 1)
-    {
-        log<level::INFO>("Initial Chassis State will be On",
-                         entry("CHASSIS_CURRENT_POWER_STATE=%s",
-                               convertForMessage(PowerState::On).c_str()));
-        server::Chassis::currentPowerState(PowerState::On);
-        server::Chassis::requestedPowerTransition(Transition::On);
-        return;
+        // Only log for unexpected error types.
+        log<level::ERR>("Error performing call to get pgood",
+                        entry("ERROR=%s", e.what()));
+        goto fail;
     }
 
 fail: