regulators: Create exception_utils namespace
This namespace contains utility functions for handling exceptions.
Signed-off-by: Shawn McCarney <shawnmm@us.ibm.com>
Change-Id: Ie945c7882cdede398af56dcd2d2c3c9c70320e92
diff --git a/phosphor-regulators/src/exception_utils.cpp b/phosphor-regulators/src/exception_utils.cpp
new file mode 100644
index 0000000..dc7f9b7
--- /dev/null
+++ b/phosphor-regulators/src/exception_utils.cpp
@@ -0,0 +1,62 @@
+/**
+ * Copyright © 2020 IBM Corporation
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#include "exception_utils.hpp"
+
+namespace phosphor::power::regulators::exception_utils
+{
+
+std::vector<std::string> getMessages(const std::exception& e)
+{
+ std::vector<std::string> messages{};
+ internal::getMessages(e, messages);
+ return messages;
+}
+
+void log(const std::exception& e)
+{
+ std::vector<std::string> messages = getMessages(e);
+ for (const std::string& message : messages)
+ {
+ journal::logErr(message);
+ }
+}
+
+namespace internal
+{
+
+void getMessages(const std::exception& e, std::vector<std::string>& messages)
+{
+ // If this exception is nested, get messages from inner exception(s)
+ try
+ {
+ std::rethrow_if_nested(e);
+ }
+ catch (const std::exception& inner)
+ {
+ getMessages(inner, messages);
+ }
+ catch (...)
+ {
+ }
+
+ // Append error message from this exception
+ messages.emplace_back(e.what());
+}
+
+} // namespace internal
+
+} // namespace phosphor::power::regulators::exception_utils
diff --git a/phosphor-regulators/src/exception_utils.hpp b/phosphor-regulators/src/exception_utils.hpp
new file mode 100644
index 0000000..825b155
--- /dev/null
+++ b/phosphor-regulators/src/exception_utils.hpp
@@ -0,0 +1,77 @@
+/**
+ * Copyright © 2020 IBM Corporation
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+#pragma once
+
+#include "journal.hpp"
+
+#include <exception>
+#include <string>
+#include <vector>
+
+/**
+ * @namespace exception_utils
+ *
+ * Contains utility functions for handling exceptions.
+ */
+namespace phosphor::power::regulators::exception_utils
+{
+
+/**
+ * Gets the error messages from the specified exception and any nested inner
+ * exceptions.
+ *
+ * If the exception contains nested inner exceptions, the messages in the
+ * returned vector will be ordered from innermost exception to outermost
+ * exception.
+ *
+ * @param e exception
+ * @return error messages from exceptions
+ */
+std::vector<std::string> getMessages(const std::exception& e);
+
+/**
+ * Logs the specified exception to the systemd journal.
+ *
+ * Gets the error messages from the specified exception and any nested inner
+ * exceptions.
+ *
+ * Logs each error message to the journal with a priority value of 'ERR'.
+ *
+ * @param e exception
+ */
+void log(const std::exception& e);
+
+/*
+ * Internal implementation details
+ */
+namespace internal
+{
+
+/**
+ * Gets the error messages from the specified exception and any nested inner
+ * exceptions.
+ *
+ * Stores the error messages in the specified vector, from innermost exception
+ * to outermost exception.
+ *
+ * @param e exception
+ * @param messages vector where error messages will be stored
+ */
+void getMessages(const std::exception& e, std::vector<std::string>& messages);
+
+} // namespace internal
+
+} // namespace phosphor::power::regulators::exception_utils
diff --git a/phosphor-regulators/src/meson.build b/phosphor-regulators/src/meson.build
index aa8921c..d8616f6 100644
--- a/phosphor-regulators/src/meson.build
+++ b/phosphor-regulators/src/meson.build
@@ -7,6 +7,7 @@
'chassis.cpp',
'config_file_parser.cpp',
'device.cpp',
+ 'exception_utils.cpp',
'id_map.cpp',
'pmbus_utils.cpp',
'system.cpp',
diff --git a/phosphor-regulators/test/exception_utils_tests.cpp b/phosphor-regulators/test/exception_utils_tests.cpp
new file mode 100644
index 0000000..6d0c4cc
--- /dev/null
+++ b/phosphor-regulators/test/exception_utils_tests.cpp
@@ -0,0 +1,149 @@
+/**
+ * Copyright © 2020 IBM Corporation
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+#include "exception_utils.hpp"
+#include "journal.hpp"
+#include "mock_journal.hpp"
+
+#include <exception>
+#include <stdexcept>
+#include <string>
+#include <vector>
+
+#include <gtest/gtest.h>
+
+using namespace phosphor::power::regulators;
+
+TEST(ExceptionUtilsTests, GetMessages)
+{
+ try
+ {
+ try
+ {
+ throw std::invalid_argument{"JSON element is not an array"};
+ }
+ catch (...)
+ {
+ std::throw_with_nested(
+ std::logic_error{"Unable to parse config file"});
+ }
+ }
+ catch (const std::exception& e)
+ {
+ std::vector<std::string> messages = exception_utils::getMessages(e);
+ EXPECT_EQ(messages.size(), 2);
+ EXPECT_EQ(messages[0], "JSON element is not an array");
+ EXPECT_EQ(messages[1], "Unable to parse config file");
+ }
+}
+
+TEST(ExceptionUtilsTests, Log)
+{
+ try
+ {
+ try
+ {
+ throw std::invalid_argument{"JSON element is not an array"};
+ }
+ catch (...)
+ {
+ std::throw_with_nested(
+ std::logic_error{"Unable to parse config file"});
+ }
+ }
+ catch (const std::exception& e)
+ {
+ journal::clear();
+ exception_utils::log(e);
+ const std::vector<std::string>& messages = journal::getErrMessages();
+ EXPECT_EQ(messages.size(), 2);
+ EXPECT_EQ(messages[0], "JSON element is not an array");
+ EXPECT_EQ(messages[1], "Unable to parse config file");
+ }
+}
+
+// Test for getMessages() function in the internal namespace
+TEST(ExceptionUtilsTests, GetMessagesInternal)
+{
+ // Test where exception is not nested
+ {
+ std::invalid_argument e{"JSON element is not an array"};
+ std::vector<std::string> messages{};
+ exception_utils::internal::getMessages(e, messages);
+ EXPECT_EQ(messages.size(), 1);
+ EXPECT_EQ(messages[0], "JSON element is not an array");
+ }
+
+ // Test where exception is nested
+ try
+ {
+ try
+ {
+ try
+ {
+ throw std::invalid_argument{"JSON element is not an array"};
+ }
+ catch (...)
+ {
+ std::throw_with_nested(
+ std::logic_error{"Unable to parse config file"});
+ }
+ }
+ catch (...)
+ {
+ std::throw_with_nested(
+ std::runtime_error{"Unable to configure regulators"});
+ }
+ }
+ catch (const std::exception& e)
+ {
+ std::vector<std::string> messages{};
+ exception_utils::internal::getMessages(e, messages);
+ EXPECT_EQ(messages.size(), 3);
+ EXPECT_EQ(messages[0], "JSON element is not an array");
+ EXPECT_EQ(messages[1], "Unable to parse config file");
+ EXPECT_EQ(messages[2], "Unable to configure regulators");
+ }
+
+ // Test where nested exception is not a child of std::exception
+ try
+ {
+ try
+ {
+ try
+ {
+ throw "JSON element is not an array";
+ }
+ catch (...)
+ {
+ std::throw_with_nested(
+ std::logic_error{"Unable to parse config file"});
+ }
+ }
+ catch (...)
+ {
+ std::throw_with_nested(
+ std::runtime_error{"Unable to configure regulators"});
+ }
+ }
+ catch (const std::exception& e)
+ {
+ std::vector<std::string> messages{};
+ exception_utils::internal::getMessages(e, messages);
+ EXPECT_EQ(messages.size(), 2);
+ EXPECT_EQ(messages[0], "Unable to parse config file");
+ EXPECT_EQ(messages[1], "Unable to configure regulators");
+ }
+}
diff --git a/phosphor-regulators/test/meson.build b/phosphor-regulators/test/meson.build
index d23bdf1..242c02a 100644
--- a/phosphor-regulators/test/meson.build
+++ b/phosphor-regulators/test/meson.build
@@ -9,6 +9,7 @@
'config_file_parser_tests.cpp',
'configuration_tests.cpp',
'device_tests.cpp',
+ 'exception_utils_tests.cpp',
'id_map_tests.cpp',
'mock_journal.cpp',
'pmbus_error_tests.cpp',