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)