regulators: Enhance exception utilities

Implement function that finds all exceptions within a nested exception
and returns them in a vector.

This function makes it easier to handle nested exceptions.  You can
iterate over them in a simple loop instead of writing recursive code.

The new function exists in the exception_utils namespace.

Signed-off-by: Shawn McCarney <shawnmm@us.ibm.com>
Change-Id: I36b135584ec6024625a572bbe0522643406245cd
diff --git a/phosphor-regulators/src/exception_utils.cpp b/phosphor-regulators/src/exception_utils.cpp
index 6f237e6..4640d0b 100644
--- a/phosphor-regulators/src/exception_utils.cpp
+++ b/phosphor-regulators/src/exception_utils.cpp
@@ -19,6 +19,13 @@
 namespace phosphor::power::regulators::exception_utils
 {
 
+std::vector<std::exception_ptr> getExceptions(std::exception_ptr eptr)
+{
+    std::vector<std::exception_ptr> exceptions;
+    internal::getExceptions(eptr, exceptions);
+    return exceptions;
+}
+
 std::vector<std::string> getMessages(const std::exception& e)
 {
     std::vector<std::string> messages{};
@@ -29,6 +36,30 @@
 namespace internal
 {
 
+void getExceptions(std::exception_ptr eptr,
+                   std::vector<std::exception_ptr>& exceptions)
+{
+    // Verify exception pointer is not null
+    if (eptr)
+    {
+        // If this exception is nested, add inner exception(s) to vector
+        try
+        {
+            std::rethrow_exception(eptr);
+        }
+        catch (const std::nested_exception& e)
+        {
+            getExceptions(e.nested_ptr(), exceptions);
+        }
+        catch (...)
+        {
+        }
+
+        // Append this exception to vector
+        exceptions.emplace_back(eptr);
+    }
+}
+
 void getMessages(const std::exception& e, std::vector<std::string>& messages)
 {
     // If this exception is nested, get messages from inner exception(s)
diff --git a/phosphor-regulators/src/exception_utils.hpp b/phosphor-regulators/src/exception_utils.hpp
index 81d700b..e90f0b6 100644
--- a/phosphor-regulators/src/exception_utils.hpp
+++ b/phosphor-regulators/src/exception_utils.hpp
@@ -30,6 +30,21 @@
 {
 
 /**
+ * Returns a vector containing the specified exception and any nested inner
+ * exceptions.
+ *
+ * If the exception contains nested inner exceptions, the returned vector will
+ * be ordered from innermost exception to outermost exception.
+ *
+ * This function makes it easier to handle nested exceptions.  You can iterate
+ * over them in a simple loop instead of writing a recursive function.
+ *
+ * @param eptr exception pointer
+ * @return vector of exceptions, from innermost to outermost
+ */
+std::vector<std::exception_ptr> getExceptions(std::exception_ptr eptr);
+
+/**
  * Gets the error messages from the specified exception and any nested inner
  * exceptions.
  *
@@ -49,6 +64,19 @@
 {
 
 /**
+ * Builds a vector containing the specified exception and any nested inner
+ * exceptions.
+ *
+ * Stores the exceptions in the specified vector, from innermost exception to
+ * outermost exception.
+ *
+ * @param eptr exception pointer
+ * @param exceptions vector where exceptions will be stored
+ */
+void getExceptions(std::exception_ptr eptr,
+                   std::vector<std::exception_ptr>& exceptions);
+
+/**
  * Gets the error messages from the specified exception and any nested inner
  * exceptions.
  *