diff options
author | Lei YU <mine260309@gmail.com> | 2017-07-12 13:12:12 +0800 |
---|---|---|
committer | Lei YU <mine260309@gmail.com> | 2017-10-16 20:40:04 +0800 |
commit | 86d80419c72699862ee498ec9e37fd02ccf3ebb1 (patch) | |
tree | 3c2ffa9107ba13d59e159ad14daf714a5fc77b4d | |
parent | 23d06af53140366be1aba634014929182897174e (diff) | |
download | phosphor-time-manager-86d80419c72699862ee498ec9e37fd02ccf3ebb1.tar.gz phosphor-time-manager-86d80419c72699862ee498ec9e37fd02ccf3ebb1.zip |
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>
-rw-r--r-- | manager.cpp | 27 | ||||
-rw-r--r-- | test/TestUtils.cpp | 32 | ||||
-rw-r--r-- | utils.cpp | 56 | ||||
-rw-r--r-- | utils.hpp | 16 |
4 files changed, 69 insertions, 62 deletions
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 @@ void Manager::checkHostOn() 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 @@ void Manager::setPropertyAsRequested(const std::string& key, 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 @@ void Manager::updateDhcpNtpSetting(const std::string& useDhcpNtp) 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 Manager::getSettings(const char* setting) const 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 time 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 @@ TEST(TestUtil, strToOwner) 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 @@ TEST(TestUtil, modeToStr) 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 @@ TEST(TestUtil, ownerToStr) 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 @@ -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 @@ const std::map<std::string, Owner> ownerMap = 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 @@ std::string getService(sdbusplus::bus::bus& bus, 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 @@ Mode strToMode(const std::string& mode) 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 @@ Owner strToOwner(const std::string& owner) 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 @@ const char* modeToStr(Mode mode) 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 @@ const char* ownerToStr(Owner owner) 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; @@ -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 @@ namespace utils { 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 @@ T getProperty(sdbusplus::bus::bus& bus, } 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>(); } |