sensorhandler: Don't fail on absent Threshold.Warning interface

Currently if a sensor has only "Threshold.Critical" interface
it would never be parsed as call to the absent "Threshold.Warning"
interface would throw and abort "getSensorThreshold" function.
Refactor "getSensorThreshold" code to fix the issue.

Signed-off-by: Konstantin Aladyshev <aladyshev22@gmail.com>
Change-Id: I7b398379272cf62f114c5158980622fb13e22e92
diff --git a/sensorhandler.cpp b/sensorhandler.cpp
index 881ea35..edd4806 100644
--- a/sensorhandler.cpp
+++ b/sensorhandler.cpp
@@ -472,7 +472,8 @@
     }
 }
 
-get_sdr::GetSensorThresholdsResponse getSensorThresholds(uint8_t sensorNum)
+get_sdr::GetSensorThresholdsResponse
+    getSensorThresholds(ipmi::Context::ptr& ctx, uint8_t sensorNum)
 {
     get_sdr::GetSensorThresholdsResponse resp{};
     constexpr auto warningThreshIntf =
@@ -480,69 +481,80 @@
     constexpr auto criticalThreshIntf =
         "xyz.openbmc_project.Sensor.Threshold.Critical";
 
-    sdbusplus::bus::bus bus{ipmid_get_sd_bus_connection()};
-
     const auto iter = ipmi::sensor::sensors.find(sensorNum);
     const auto info = iter->second;
 
-    auto service = ipmi::getService(bus, info.sensorInterface, info.sensorPath);
-
-    auto warnThresholds = ipmi::getAllDbusProperties(
-        bus, service, info.sensorPath, warningThreshIntf);
-
-    double warnLow = std::visit(ipmi::VariantToDoubleVisitor(),
-                                warnThresholds["WarningLow"]);
-    double warnHigh = std::visit(ipmi::VariantToDoubleVisitor(),
-                                 warnThresholds["WarningHigh"]);
-
-    if (std::isfinite(warnLow))
+    std::string service;
+    boost::system::error_code ec;
+    ec = ipmi::getService(ctx, info.sensorInterface, info.sensorPath, service);
+    if (ec)
     {
-        warnLow *= std::pow(10, info.scale - info.exponentR);
-        resp.lowerNonCritical = static_cast<uint8_t>(
-            (warnLow - info.scaledOffset) / info.coefficientM);
-        resp.validMask |= static_cast<uint8_t>(
-            ipmi::sensor::ThresholdMask::NON_CRITICAL_LOW_MASK);
+        return resp;
     }
 
-    if (std::isfinite(warnHigh))
+    ipmi::PropertyMap warnThresholds;
+    ec = ipmi::getAllDbusProperties(ctx, service, info.sensorPath,
+                                    warningThreshIntf, warnThresholds);
+    if (!ec)
     {
-        warnHigh *= std::pow(10, info.scale - info.exponentR);
-        resp.upperNonCritical = static_cast<uint8_t>(
-            (warnHigh - info.scaledOffset) / info.coefficientM);
-        resp.validMask |= static_cast<uint8_t>(
-            ipmi::sensor::ThresholdMask::NON_CRITICAL_HIGH_MASK);
+        double warnLow = std::visit(ipmi::VariantToDoubleVisitor(),
+                                    warnThresholds["WarningLow"]);
+        double warnHigh = std::visit(ipmi::VariantToDoubleVisitor(),
+                                     warnThresholds["WarningHigh"]);
+
+        if (std::isfinite(warnLow))
+        {
+            warnLow *= std::pow(10, info.scale - info.exponentR);
+            resp.lowerNonCritical = static_cast<uint8_t>(
+                (warnLow - info.scaledOffset) / info.coefficientM);
+            resp.validMask |= static_cast<uint8_t>(
+                ipmi::sensor::ThresholdMask::NON_CRITICAL_LOW_MASK);
+        }
+
+        if (std::isfinite(warnHigh))
+        {
+            warnHigh *= std::pow(10, info.scale - info.exponentR);
+            resp.upperNonCritical = static_cast<uint8_t>(
+                (warnHigh - info.scaledOffset) / info.coefficientM);
+            resp.validMask |= static_cast<uint8_t>(
+                ipmi::sensor::ThresholdMask::NON_CRITICAL_HIGH_MASK);
+        }
     }
 
-    auto critThresholds = ipmi::getAllDbusProperties(
-        bus, service, info.sensorPath, criticalThreshIntf);
-
-    double critLow = std::visit(ipmi::VariantToDoubleVisitor(),
-                                critThresholds["CriticalLow"]);
-    double critHigh = std::visit(ipmi::VariantToDoubleVisitor(),
-                                 critThresholds["CriticalHigh"]);
-
-    if (std::isfinite(critLow))
+    ipmi::PropertyMap critThresholds;
+    ec = ipmi::getAllDbusProperties(ctx, service, info.sensorPath,
+                                    criticalThreshIntf, critThresholds);
+    if (!ec)
     {
-        critLow *= std::pow(10, info.scale - info.exponentR);
-        resp.lowerCritical = static_cast<uint8_t>(
-            (critLow - info.scaledOffset) / info.coefficientM);
-        resp.validMask |= static_cast<uint8_t>(
-            ipmi::sensor::ThresholdMask::CRITICAL_LOW_MASK);
-    }
+        double critLow = std::visit(ipmi::VariantToDoubleVisitor(),
+                                    critThresholds["CriticalLow"]);
+        double critHigh = std::visit(ipmi::VariantToDoubleVisitor(),
+                                     critThresholds["CriticalHigh"]);
 
-    if (std::isfinite(critHigh))
-    {
-        critHigh *= std::pow(10, info.scale - info.exponentR);
-        resp.upperCritical = static_cast<uint8_t>(
-            (critHigh - info.scaledOffset) / info.coefficientM);
-        resp.validMask |= static_cast<uint8_t>(
-            ipmi::sensor::ThresholdMask::CRITICAL_HIGH_MASK);
+        if (std::isfinite(critLow))
+        {
+            critLow *= std::pow(10, info.scale - info.exponentR);
+            resp.lowerCritical = static_cast<uint8_t>(
+                (critLow - info.scaledOffset) / info.coefficientM);
+            resp.validMask |= static_cast<uint8_t>(
+                ipmi::sensor::ThresholdMask::CRITICAL_LOW_MASK);
+        }
+
+        if (std::isfinite(critHigh))
+        {
+            critHigh *= std::pow(10, info.scale - info.exponentR);
+            resp.upperCritical = static_cast<uint8_t>(
+                (critHigh - info.scaledOffset) / info.coefficientM);
+            resp.validMask |= static_cast<uint8_t>(
+                ipmi::sensor::ThresholdMask::CRITICAL_HIGH_MASK);
+        }
     }
 
     return resp;
 }
 
 /** @brief implements the get sensor thresholds command
+ *  @param ctx - IPMI context pointer
  *  @param sensorNum - sensor number
  *
  *  @returns IPMI completion code plus response data
@@ -562,7 +574,7 @@
               uint8_t, // upperCritical
               uint8_t  // upperNonRecoverable
               >
-    ipmiSensorGetSensorThresholds(uint8_t sensorNum)
+    ipmiSensorGetSensorThresholds(ipmi::Context::ptr& ctx, uint8_t sensorNum)
 {
     constexpr auto valueInterface = "xyz.openbmc_project.Sensor.Value";
 
@@ -583,14 +595,7 @@
     }
 
     get_sdr::GetSensorThresholdsResponse resp{};
-    try
-    {
-        resp = getSensorThresholds(sensorNum);
-    }
-    catch (std::exception& e)
-    {
-        // Mask if the property is not present
-    }
+    resp = getSensorThresholds(ctx, sensorNum);
 
     return ipmi::responseSuccess(resp.validMask, resp.lowerNonCritical,
                                  resp.lowerCritical, resp.lowerNonRecoverable,