diff options
author | Lei YU <mine260309@gmail.com> | 2018-07-13 15:14:56 +0800 |
---|---|---|
committer | Lei YU <mine260309@gmail.com> | 2018-09-12 10:45:04 +0800 |
commit | 86c83f384b54bcbb716508103ae2ff3f3b4589aa (patch) | |
tree | b776b0faab0f55dd25b562d1fc63a88e71624972 | |
parent | bd024395f6e34f4bc5eff4941991abc572e9707e (diff) | |
download | phosphor-time-manager-86c83f384b54bcbb716508103ae2ff3f3b4589aa.tar.gz phosphor-time-manager-86c83f384b54bcbb716508103ae2ff3f3b4589aa.zip |
Throw runtime_error instead of MethodEror
As suggested, when error occurs in getService/getProperty, it means
runtime error, so the code is better to throw std::runtime_error instead
of a sdbusplus exception.
Tested: Build the code correctly.
Change-Id: Iddca72ac1fdd07ed6b731b6509eebbfa9d2579d3
Signed-off-by: Lei YU <mine260309@gmail.com>
-rw-r--r-- | Makefile.am | 15 | ||||
-rw-r--r-- | elog-errors.hpp | 116 | ||||
-rw-r--r-- | utils.cpp | 49 | ||||
-rw-r--r-- | utils.hpp | 25 | ||||
-rw-r--r-- | xyz/openbmc_project/Time/Internal.errors.yaml | 3 | ||||
-rw-r--r-- | xyz/openbmc_project/Time/Internal.metadata.yaml | 10 |
6 files changed, 31 insertions, 187 deletions
diff --git a/Makefile.am b/Makefile.am index 09e8807..fce04e0 100644 --- a/Makefile.am +++ b/Makefile.am @@ -4,13 +4,6 @@ sbin_PROGRAMS = phosphor-timemanager noinst_LTLIBRARIES = libtimemanager.la -generated_source = xyz/openbmc_project/Time/Internal/error.cpp - -BUILT_SOURCES = ${generated_source} \ - xyz/openbmc_project/Time/Internal/error.hpp - -CLEANFILES = ${BUILT_SOURCES} - libtimemanager_la_SOURCES = \ epoch_base.cpp \ bmc_epoch.cpp \ @@ -38,12 +31,4 @@ phosphor_timemanager_CXXFLAGS = $(generic_cxx_flags) phosphor_timemanager_LDFLAGS = $(generic_ld_flags) -xyz/openbmc_project/Time/Internal/error.hpp: ${top_srcdir}/xyz/openbmc_project/Time/Internal.errors.yaml - @mkdir -p `dirname $@` - $(SDBUSPLUSPLUS) -r $(top_srcdir) error exception-header xyz.openbmc_project.Time.Internal > $@ - -xyz/openbmc_project/Time/Internal/error.cpp: ${top_srcdir}/xyz/openbmc_project/Time/Internal.errors.yaml - @mkdir -p `dirname $@` - $(SDBUSPLUSPLUS) -r $(top_srcdir) error exception-cpp xyz.openbmc_project.Time.Internal> $@ - SUBDIRS = . test diff --git a/elog-errors.hpp b/elog-errors.hpp deleted file mode 100644 index aa8d581..0000000 --- a/elog-errors.hpp +++ /dev/null @@ -1,116 +0,0 @@ -// This file was autogenerated. Do not edit! -// See elog-gen.py for more details -#pragma once - -#include <string> -#include <tuple> -#include <type_traits> -#include <sdbusplus/exception.hpp> -#include <phosphor-logging/log.hpp> -#include <phosphor-logging/elog.hpp> - -namespace sdbusplus -{ -namespace xyz -{ -namespace openbmc_project -{ -namespace Time -{ -namespace Internal -{ -namespace Error -{ - struct MethodError; -} // namespace Error -} // namespace Internal -} // namespace Time -} // namespace openbmc_project -} // namespace xyz -} // namespace sdbusplus - - -namespace phosphor -{ - -namespace logging -{ - -namespace xyz -{ -namespace openbmc_project -{ -namespace Time -{ -namespace Internal -{ -namespace _MethodError -{ - -struct METHOD_NAME -{ - static constexpr auto str = "METHOD_NAME=%s"; - static constexpr auto str_short = "METHOD_NAME"; - using type = std::tuple<std::decay_t<decltype(str)>,const char*>; - explicit constexpr METHOD_NAME(const char* a) : _entry(entry(str, a)) {}; - type _entry; -}; -struct PATH -{ - static constexpr auto str = "PATH=%s"; - static constexpr auto str_short = "PATH"; - using type = std::tuple<std::decay_t<decltype(str)>,const char*>; - explicit constexpr PATH(const char* a) : _entry(entry(str, a)) {}; - type _entry; -}; -struct INTERFACE -{ - static constexpr auto str = "INTERFACE=%s"; - static constexpr auto str_short = "INTERFACE"; - using type = std::tuple<std::decay_t<decltype(str)>,const char*>; - explicit constexpr INTERFACE(const char* a) : _entry(entry(str, a)) {}; - type _entry; -}; -struct MISC -{ - static constexpr auto str = "MISC=%s"; - static constexpr auto str_short = "MISC"; - using type = std::tuple<std::decay_t<decltype(str)>,const char*>; - explicit constexpr MISC(const char* a) : _entry(entry(str, a)) {}; - type _entry; -}; - -} // namespace _MethodError - -struct MethodError -{ - static constexpr auto L = level::ERR; - using METHOD_NAME = _MethodError::METHOD_NAME; - using PATH = _MethodError::PATH; - using INTERFACE = _MethodError::INTERFACE; - using MISC = _MethodError::MISC; - using metadata_types = std::tuple<METHOD_NAME, PATH, INTERFACE, MISC>; - -}; - -} // namespace Internal -} // namespace Time -} // namespace openbmc_project -} // namespace xyz - - -namespace details -{ - -template <> -struct map_exception_type<sdbusplus::xyz::openbmc_project::Time::Internal::Error::MethodError> -{ - using type = xyz::openbmc_project::Time::Internal::MethodError; -}; - -} - - -} // namespace logging - -} // namespace phosphor @@ -1,9 +1,6 @@ #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 @@ -21,9 +18,6 @@ constexpr auto MAPPER_INTERFACE = "xyz.openbmc_project.ObjectMapper"; namespace utils { -using InvalidArgumentError = - sdbusplus::xyz::openbmc_project::Common::Error::InvalidArgument; - using namespace phosphor::logging; std::string getService(sdbusplus::bus::bus& bus, @@ -36,32 +30,31 @@ std::string getService(sdbusplus::bus::bus& bus, "GetObject"); mapper.append(path, std::vector<std::string>({interface})); - auto mapperResponseMsg = bus.call(mapper); - - if (mapperResponseMsg.is_method_error()) + try { - using namespace xyz::openbmc_project::Time::Internal; - elog<MethodErr>(MethodError::METHOD_NAME("GetObject"), - MethodError::PATH(path), - MethodError::INTERFACE(interface), - MethodError::MISC({})); + auto mapperResponseMsg = bus.call(mapper); + + std::vector<std::pair<std::string, std::vector<std::string>>> + mapperResponse; + mapperResponseMsg.read(mapperResponse); + if (mapperResponse.empty()) + { + log<level::ERR>("Error reading mapper response"); + throw std::runtime_error("Error reading mapper response"); + } + if (mapperResponse.size() < 1){ + return ""; + } + return mapperResponse[0].first; } - - std::vector<std::pair<std::string, std::vector<std::string>>> - mapperResponse; - mapperResponseMsg.read(mapperResponse); - if (mapperResponse.empty()) + catch (const sdbusplus::exception::SdBusError& ex) { - 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")); - } - if (mapperResponse.size() < 1){ - return ""; + log<level::ERR>("Mapper call failed", + entry("METHOD=%d", "GetObject"), + entry("PATH=%s", path), + entry("INTERFACE=%s", interface)); + throw std::runtime_error("Mapper call failed"); } - return mapperResponse[0].first; } Mode strToMode(const std::string& mode) @@ -1,10 +1,7 @@ #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> @@ -18,8 +15,6 @@ namespace utils { using namespace phosphor::logging; -using MethodErr = - sdbusplus::xyz::openbmc_project::Time::Internal::Error::MethodError; /** @brief Read data with type T from file * @@ -71,26 +66,26 @@ T getProperty(sdbusplus::bus::bus& bus, const char* interface, const char* propertyName) { - sdbusplus::message::variant<T> value{}; auto method = bus.new_method_call(service, path, "org.freedesktop.DBus.Properties", "Get"); method.append(interface, propertyName); - auto reply = bus.call(method); - if (reply) + try { + sdbusplus::message::variant<T> value{}; + auto reply = bus.call(method); reply.read(value); + return value.template get<T>(); } - else + catch (const sdbusplus::exception::SdBusError& ex) { - using namespace xyz::openbmc_project::Time::Internal; - elog<MethodErr>(MethodError::METHOD_NAME("Get"), - MethodError::PATH(path), - MethodError::INTERFACE(interface), - MethodError::MISC(propertyName)); + log<level::ERR>("GetProperty call failed", + entry("PATH=%s", path), + entry("INTERFACE=%s", interface), + entry("PROPERTY=%s", propertyName)); + throw std::runtime_error("GetProperty call failed"); } - return value.template get<T>(); } /** @brief Get service name from object path and interface diff --git a/xyz/openbmc_project/Time/Internal.errors.yaml b/xyz/openbmc_project/Time/Internal.errors.yaml deleted file mode 100644 index a2502ab..0000000 --- a/xyz/openbmc_project/Time/Internal.errors.yaml +++ /dev/null @@ -1,3 +0,0 @@ -#xyz.openbmc_project.Time.Internal.MethodError -- name: MethodError - description: Failed to invoke Dbus method diff --git a/xyz/openbmc_project/Time/Internal.metadata.yaml b/xyz/openbmc_project/Time/Internal.metadata.yaml deleted file mode 100644 index 0f16c11..0000000 --- a/xyz/openbmc_project/Time/Internal.metadata.yaml +++ /dev/null @@ -1,10 +0,0 @@ -- name: MethodError - meta: - - str: "METHOD_NAME=%s" - type: string - - str: "PATH=%s" - type: string - - str: "INTERFACE=%s" - type: string - - str: "MISC=%s" - type: string |