Remove is_method_error method
Remove the usage of is_method_error()[1], and add try-catch to handle
D-Bus exceptions around mapper call.
[1]https://github.com/openbmc/sdbusplus/commit/079fb85a398d90800935e3985bb1266a7530a26e#diff-945669e8bd9cab4ecc83a574a732921281b2c79eb8bba65efff11736ad18f92bR237-R240
Signed-off-by: George Liu <liuxiwei@inspur.com>
Change-Id: I85192219c1c34cf5fd6c6aca06a8b207d7e06697
diff --git a/dcmihandler.cpp b/dcmihandler.cpp
index d34ab31..ca2ee7d 100644
--- a/dcmihandler.cpp
+++ b/dcmihandler.cpp
@@ -81,17 +81,19 @@
"org.freedesktop.DBus.Properties", "Get");
method.append(PCAP_INTERFACE, POWER_CAP_PROP);
- auto reply = bus.call(method);
- if (reply.is_method_error())
+ std::variant<uint32_t> pcap;
+ try
{
- log<level::ERR>("Error in getPcap prop");
+ auto reply = bus.call(method);
+ reply.read(pcap);
+ return std::get<uint32_t>(pcap);
+ }
+ catch (const std::exception& e)
+ {
+ log<level::ERR>("Error in getPcap prop", entry("ERROR=%s", e.what()));
elog<InternalFailure>();
}
- std::variant<uint32_t> pcap;
- reply.read(pcap);
-
- return std::get<uint32_t>(pcap);
}
bool getPcapEnabled(sdbusplus::bus_t& bus)
@@ -102,17 +104,20 @@
"org.freedesktop.DBus.Properties", "Get");
method.append(PCAP_INTERFACE, POWER_CAP_ENABLE_PROP);
- auto reply = bus.call(method);
- if (reply.is_method_error())
+ std::variant<bool> pcapEnabled;
+ try
{
- log<level::ERR>("Error in getPcapEnabled prop");
+ auto reply = bus.call(method);
+ reply.read(pcapEnabled);
+ return std::get<bool>(pcapEnabled);
+ }
+ catch (const std::exception& e)
+ {
+ log<level::ERR>("Error in getPcapEnabled prop",
+ entry("ERROR=%s", e.what()));
elog<InternalFailure>();
}
- std::variant<bool> pcapEnabled;
- reply.read(pcapEnabled);
-
- return std::get<bool>(pcapEnabled);
}
void setPcap(sdbusplus::bus_t& bus, const uint32_t powerCap)
@@ -125,11 +130,14 @@
method.append(PCAP_INTERFACE, POWER_CAP_PROP);
method.append(std::variant<uint32_t>(powerCap));
- auto reply = bus.call(method);
-
- if (reply.is_method_error())
+ try
{
- log<level::ERR>("Error in setPcap property");
+ auto reply = bus.call(method);
+ }
+ catch (const std::exception& e)
+ {
+ log<level::ERR>("Error in setPcap property",
+ entry("ERROR=%s", e.what()));
elog<InternalFailure>();
}
}
@@ -144,11 +152,14 @@
method.append(PCAP_INTERFACE, POWER_CAP_ENABLE_PROP);
method.append(std::variant<bool>(enabled));
- auto reply = bus.call(method);
-
- if (reply.is_method_error())
+ try
{
- log<level::ERR>("Error in setPcapEnabled property");
+ auto reply = bus.call(method);
+ }
+ catch (const std::exception& e)
+ {
+ log<level::ERR>("Error in setPcapEnabled property",
+ entry("ERROR=%s", e.what()));
elog<InternalFailure>();
}
}
@@ -170,18 +181,20 @@
mapperCall.append(depth);
mapperCall.append(std::vector<std::string>({dcmi::assetTagIntf}));
- auto mapperReply = bus.call(mapperCall);
- if (mapperReply.is_method_error())
+ try
{
- log<level::ERR>("Error in mapper call");
- elog<InternalFailure>();
+ auto mapperReply = bus.call(mapperCall);
+ mapperReply.read(objectTree);
+
+ if (objectTree.empty())
+ {
+ log<level::ERR>("AssetTag property is not populated");
+ elog<InternalFailure>();
+ }
}
-
- mapperReply.read(objectTree);
-
- if (objectTree.empty())
+ catch (const std::exception& e)
{
- log<level::ERR>("AssetTag property is not populated");
+ log<level::ERR>("Error in mapper call", entry("ERROR=%s", e.what()));
elog<InternalFailure>();
}
}
@@ -201,17 +214,19 @@
method.append(dcmi::assetTagIntf);
method.append(dcmi::assetTagProp);
- auto reply = bus.call(method);
- if (reply.is_method_error())
+ std::variant<std::string> assetTag;
+ try
{
- log<level::ERR>("Error in reading asset tag");
+ auto reply = bus.call(method);
+ reply.read(assetTag);
+ return std::get<std::string>(assetTag);
+ }
+ catch (const std::exception& e)
+ {
+ log<level::ERR>("Error in reading asset tag",
+ entry("ERROR=%s", e.what()));
elog<InternalFailure>();
}
-
- std::variant<std::string> assetTag;
- reply.read(assetTag);
-
- return std::get<std::string>(assetTag);
}
void writeAssetTag(const std::string& assetTag)
@@ -230,10 +245,14 @@
method.append(dcmi::assetTagProp);
method.append(std::variant<std::string>(assetTag));
- auto reply = bus.call(method);
- if (reply.is_method_error())
+ try
{
- log<level::ERR>("Error in writing asset tag");
+ auto reply = bus.call(method);
+ }
+ catch (const std::exception& e)
+ {
+ log<level::ERR>("Error in writing asset tag",
+ entry("ERROR=%s", e.what()));
elog<InternalFailure>();
}
}