Use elog to throw exceptions

Use elog to throw exceptions and update unit tests

Change-Id: I338ded4403b3b559a84da311eda5ee15e712569a
Signed-off-by: Lei YU <mine260309@gmail.com>
diff --git a/manager.cpp b/manager.cpp
index 03992b1..0fedd09 100644
--- a/manager.cpp
+++ b/manager.cpp
@@ -1,7 +1,10 @@
 #include "manager.hpp"
 #include "utils.hpp"
 
+#include <phosphor-logging/elog.hpp>
+#include <phosphor-logging/elog-errors.hpp>
 #include <phosphor-logging/log.hpp>
+#include <xyz/openbmc_project/Common/error.hpp>
 
 namespace rules = sdbusplus::bus::match::rules;
 
@@ -92,11 +95,6 @@
     std::string powerService = utils::getService(bus,
                                                  POWER_PATH,
                                                  POWER_INTERFACE);
-    if (powerService.empty())
-    {
-        log<level::ERR>("Failed to get power service, assume host is off");
-        return;
-    }
 
     int pgood = utils::getProperty<int>(bus,
                                         powerService.c_str(),
@@ -180,8 +178,12 @@
     else
     {
         // The key shall be already the supported one
-        // TODO: use elog API
-        assert(false);
+        using InvalidArgumentError =
+            sdbusplus::xyz::openbmc_project::Common::Error::InvalidArgument;
+        using namespace xyz::openbmc_project::Common;
+        elog<InvalidArgumentError>(
+            InvalidArgument::ARGUMENT_NAME(key.c_str()),
+            InvalidArgument::ARGUMENT_VALUE(value.c_str()));
     }
 }
 
@@ -221,11 +223,6 @@
     std::string networkService = utils::getService(bus,
                                                    OBMC_NETWORK_PATH,
                                                    OBMC_NETWORK_INTERFACE);
-    if (networkService.empty())
-    {
-        log<level::ERR>("Failed to get network service, ignore dhcp ntp");
-        return;
-    }
 
     auto method = bus.new_method_call(networkService.c_str(),
                                       OBMC_NETWORK_PATH,
@@ -348,12 +345,6 @@
     std::string settingsService = utils::getService(bus,
                                                     SETTINGS_PATH,
                                                     SETTINGS_INTERFACE);
-    if (settingsService.empty())
-    {
-        log<level::ERR>("Failed to get settings service, unable to get setting",
-                        entry("SETTING=%s", setting));
-        return {};
-    }
 
     return utils::getProperty<std::string>(bus,
                                            settingsService.c_str(),
diff --git a/test/TestUtils.cpp b/test/TestUtils.cpp
index fc345f2..16ccd9c 100644
--- a/test/TestUtils.cpp
+++ b/test/TestUtils.cpp
@@ -1,6 +1,7 @@
 #include "utils.hpp"
 
 #include <gtest/gtest.h>
+#include <xyz/openbmc_project/Common/error.hpp>
 
 namespace phosphor
 {
@@ -9,16 +10,18 @@
 namespace utils
 {
 
+using InvalidArgument =
+    sdbusplus::xyz::openbmc_project::Common::Error::InvalidArgument;
+
 TEST(TestUtil, strToMode)
 {
     EXPECT_EQ(Mode::NTP, strToMode("NTP"));
     EXPECT_EQ(Mode::MANUAL, strToMode("MANUAL"));
 
-    // All unrecognized strings result in assertion
-    // TODO: use EXPECT_THROW once the code uses elog
-    EXPECT_DEATH(strToMode(""), "");
-    EXPECT_DEATH(strToMode("Manual"), "");
-    EXPECT_DEATH(strToMode("whatever"), "");
+    // All unrecognized strings result in InvalidArgument exception
+    EXPECT_THROW(strToMode(""), InvalidArgument);
+    EXPECT_THROW(strToMode("Manual"), InvalidArgument);
+    EXPECT_THROW(strToMode("whatever"), InvalidArgument);
 }
 
 
@@ -29,11 +32,10 @@
     EXPECT_EQ(Owner::SPLIT, strToOwner("SPLIT"));
     EXPECT_EQ(Owner::BOTH, strToOwner("BOTH"));
 
-    // All unrecognized strings result in assertion
-    // TODO: use EXPECT_THROW once the code uses elog
-    EXPECT_DEATH(strToOwner(""), "");
-    EXPECT_DEATH(strToOwner("Split"), "");
-    EXPECT_DEATH(strToOwner("xyz"), "");
+    // All unrecognized strings result in InvalidArgument exception
+    EXPECT_THROW(strToOwner(""), InvalidArgument);
+    EXPECT_THROW(strToOwner("Split"), InvalidArgument);
+    EXPECT_THROW(strToOwner("xyz"), InvalidArgument);
 }
 
 TEST(TestUtil, modeToStr)
@@ -41,9 +43,8 @@
     EXPECT_EQ("NTP", modeToStr(Mode::NTP));
     EXPECT_EQ("MANUAL", modeToStr(Mode::MANUAL));
 
-    // All unrecognized enums result in assertion
-    // TODO: use EXPECT_THROW once the code uses elog
-    EXPECT_DEATH(modeToStr(static_cast<Mode>(100)), "");
+    // All unrecognized strings result in InvalidArgument exception
+    EXPECT_THROW(modeToStr(static_cast<Mode>(100)), InvalidArgument);
 }
 
 TEST(TestUtil, ownerToStr)
@@ -53,9 +54,8 @@
     EXPECT_EQ("SPLIT", ownerToStr(Owner::SPLIT));
     EXPECT_EQ("BOTH", ownerToStr(Owner::BOTH));
 
-    // All unrecognized enums result in assertion
-    // TODO: use EXPECT_THROW once the code uses elog
-    EXPECT_DEATH(ownerToStr(static_cast<Owner>(100)), "");
+    // All unrecognized strings result in InvalidArgument exception
+    EXPECT_THROW(ownerToStr(static_cast<Owner>(100)), InvalidArgument);
 }
 
 } // namespace utils
diff --git a/utils.cpp b/utils.cpp
index 6df6ed5..57c3b5f 100644
--- a/utils.cpp
+++ b/utils.cpp
@@ -1,6 +1,9 @@
 #include "utils.hpp"
 
+#include <phosphor-logging/elog.hpp>
+#include <phosphor-logging/elog-errors.hpp>
 #include <phosphor-logging/log.hpp>
+#include <xyz/openbmc_project/Common/error.hpp>
 
 
 namespace phosphor
@@ -34,6 +37,9 @@
 namespace utils
 {
 
+using InvalidArgumentError =
+    sdbusplus::xyz::openbmc_project::Common::Error::InvalidArgument;
+
 using namespace phosphor::logging;
 
 std::string getService(sdbusplus::bus::bus& bus,
@@ -50,22 +56,22 @@
 
     if (mapperResponseMsg.is_method_error())
     {
-        // TODO: define repo specific errors and use elog report()
-        log<level::ERR>("Error in mapper call",
-                        entry("PATH=%s", path),
-                        entry("INTERFACE=%s", interface));
-        return {};
+        using namespace xyz::openbmc_project::Time::Internal;
+        elog<MethodErr>(MethodError::METHOD_NAME("GetObject"),
+                          MethodError::PATH(path),
+                          MethodError::INTERFACE(interface),
+                          MethodError::MISC({}));
     }
 
     std::map<std::string, std::vector<std::string>> mapperResponse;
     mapperResponseMsg.read(mapperResponse);
     if (mapperResponse.empty())
     {
-        // TODO: define repo specific errors and use elog report()
-        log<level::ERR>("Error reading mapper response",
-                        entry("PATH=%s", path),
-                        entry("INTERFACE=%s", interface));
-        return {};
+        using namespace xyz::openbmc_project::Time::Internal;
+        elog<MethodErr>(MethodError::METHOD_NAME("GetObject"),
+                          MethodError::PATH(path),
+                          MethodError::INTERFACE(interface),
+                          MethodError::MISC("Error reading mapper response"));
     }
 
     return mapperResponse.begin()->first;
@@ -76,10 +82,10 @@
     auto it = modeMap.find(mode);
     if (it == modeMap.end())
     {
-        log<level::ERR>("Unrecognized mode",
-                        entry("%s", mode.c_str()));
-        // TODO: use elog to throw exceptions
-        assert(0);
+        using namespace xyz::openbmc_project::Common;
+        elog<InvalidArgumentError>(
+            InvalidArgument::ARGUMENT_NAME("TimeMode"),
+            InvalidArgument::ARGUMENT_VALUE(mode.c_str()));
     }
     return it->second;
 }
@@ -89,10 +95,10 @@
     auto it = ownerMap.find(owner);
     if (it == ownerMap.end())
     {
-        log<level::ERR>("Unrecognized owner",
-                        entry("%s", owner.c_str()));
-        // TODO: use elog to throw exceptions
-        assert(0);
+        using namespace xyz::openbmc_project::Common;
+        elog<InvalidArgumentError>(
+            InvalidArgument::ARGUMENT_NAME("TimeOwner"),
+            InvalidArgument::ARGUMENT_VALUE(owner.c_str()));
     }
     return it->second;
 }
@@ -109,8 +115,11 @@
         ret = "MANUAL";
         break;
     default:
-        // TODO: use elog to throw exceptions
-        assert(0);
+        using namespace xyz::openbmc_project::Common;
+        elog<InvalidArgumentError>(
+            InvalidArgument::ARGUMENT_NAME("Mode"),
+            InvalidArgument::ARGUMENT_VALUE(
+                std::to_string(static_cast<int>(mode)).c_str()));
         break;
     }
     return ret;
@@ -134,8 +143,11 @@
         ret = "BOTH";
         break;
     default:
-        // TODO: use elog to throw exceptions
-        assert(0);
+        using namespace xyz::openbmc_project::Common;
+        elog<InvalidArgumentError>(
+            InvalidArgument::ARGUMENT_NAME("Owner"),
+            InvalidArgument::ARGUMENT_VALUE(
+                std::to_string(static_cast<int>(owner)).c_str()));
         break;
     }
     return ret;
diff --git a/utils.hpp b/utils.hpp
index ea2e3e4..87bc2bf 100644
--- a/utils.hpp
+++ b/utils.hpp
@@ -1,7 +1,10 @@
 #pragma once
 
+#include "elog-errors.hpp"
 #include "types.hpp"
+#include "xyz/openbmc_project/Time/Internal/error.hpp"
 
+#include <phosphor-logging/elog.hpp>
 #include <phosphor-logging/log.hpp>
 #include <sdbusplus/bus.hpp>
 
@@ -15,6 +18,8 @@
 {
 
 using namespace phosphor::logging;
+using MethodErr =
+    sdbusplus::xyz::openbmc_project::Time::Internal::Error::MethodError;
 
 /** @brief Read data with type T from file
  *
@@ -79,12 +84,11 @@
     }
     else
     {
-        // TODO: use elog to throw exception
-        log<level::ERR>("Failed to get property",
-                        entry("SERVICE=%s", service),
-                        entry("PATH=%s", path),
-                        entry("INTERFACE=%s", interface),
-                        entry("PROPERTY=%s", propertyName));
+        using namespace xyz::openbmc_project::Time::Internal;
+        elog<MethodErr>(MethodError::METHOD_NAME("Get"),
+                          MethodError::PATH(path),
+                          MethodError::INTERFACE(interface),
+                          MethodError::MISC(propertyName));
     }
     return value.template get<T>();
 }