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/discover_system_state.cpp b/discover_system_state.cpp
index 92d3416..3a38152 100644
--- a/discover_system_state.cpp
+++ b/discover_system_state.cpp
@@ -4,6 +4,7 @@
#include <string>
#include <config.h>
#include <systemd/sd-bus.h>
+#include <sdbusplus/exception.hpp>
#include <sdbusplus/server.hpp>
#include <phosphor-logging/log.hpp>
#include <phosphor-logging/elog-errors.hpp>
@@ -22,6 +23,7 @@
using namespace phosphor::logging;
using namespace sdbusplus::xyz::openbmc_project::Common::Error;
using namespace sdbusplus::xyz::openbmc_project::Control::Power::server;
+using sdbusplus::exception::SdBusError;
constexpr auto MAPPER_BUSNAME = "xyz.openbmc_project.ObjectMapper";
constexpr auto MAPPER_PATH = "/xyz/openbmc_project/object_mapper";
@@ -36,23 +38,27 @@
MAPPER_INTERFACE, "GetObject");
mapper.append(path, std::vector<std::string>({interface}));
- auto mapperResponseMsg = bus.call(mapper);
-
- if (mapperResponseMsg.is_method_error())
- {
- log<level::ERR>("Error in mapper call", entry("PATH=%s", path.c_str()),
- entry("INTERFACE=%s", interface.c_str()));
- throw std::runtime_error("Error in mapper call");
- }
std::map<std::string, std::vector<std::string>> mapperResponse;
- mapperResponseMsg.read(mapperResponse);
- if (mapperResponse.empty())
+ try
{
- log<level::ERR>("Error reading mapper response",
+ auto mapperResponseMsg = bus.call(mapper);
+
+ mapperResponseMsg.read(mapperResponse);
+ if (mapperResponse.empty())
+ {
+ log<level::ERR>("Error reading mapper response",
+ entry("PATH=%s", path.c_str()),
+ entry("INTERFACE=%s", interface.c_str()));
+ throw std::runtime_error("Error reading mapper response");
+ }
+ }
+ catch (const SdBusError& e)
+ {
+ log<level::ERR>("Error in mapper call", entry("ERROR=%s", e.what()),
entry("PATH=%s", path.c_str()),
entry("INTERFACE=%s", interface.c_str()));
- throw std::runtime_error("Error reading mapper response");
+ throw;
}
return mapperResponse.begin()->first;
@@ -68,16 +74,18 @@
PROPERTY_INTERFACE, "Get");
method.append(interface, propertyName);
- auto reply = bus.call(method);
- if (reply.is_method_error())
+ try
{
- log<level::ERR>("Error in property Get",
- entry("PROPERTY=%s", propertyName.c_str()));
- throw std::runtime_error("Error in property Get");
+ auto reply = bus.call(method);
+ reply.read(property);
}
-
- reply.read(property);
+ catch (const SdBusError& e)
+ {
+ log<level::ERR>("Error in property Get", entry("ERROR=%s", e.what()),
+ entry("PROPERTY=%s", propertyName.c_str()));
+ throw;
+ }
if (sdbusplus::message::variant_ns::get<std::string>(property).empty())
{
@@ -148,15 +156,20 @@
settings.powerRestorePolicy.c_str(), "org.freedesktop.DBus.Properties",
"Get");
method.append(powerRestoreIntf, "PowerRestorePolicy");
- auto reply = bus.call(method);
- if (reply.is_method_error())
+
+ sdbusplus::message::variant<std::string> result;
+ try
{
- log<level::ERR>("Error in PowerRestorePolicy Get");
+ auto reply = bus.call(method);
+ reply.read(result);
+ }
+ catch (const SdBusError& e)
+ {
+ log<level::ERR>("Error in PowerRestorePolicy Get",
+ entry("ERROR=%s", e.what()));
elog<InternalFailure>();
}
- sdbusplus::message::variant<std::string> result;
- reply.read(result);
auto powerPolicy = sdbusplus::message::variant_ns::get<std::string>(result);
log<level::INFO>("Host power is off, checking power policy",