From 48aeb32ee55709c1a6f3642f97be69e987e5d2cf Mon Sep 17 00:00:00 2001 From: Patrick Wuttke Date: Mon, 16 Dec 2024 20:53:17 +0100 Subject: [PATCH] Added walkExceptionCause helper function. --- source/mijin/util/exception.hpp | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) diff --git a/source/mijin/util/exception.hpp b/source/mijin/util/exception.hpp index 2074f84..7ad87e3 100644 --- a/source/mijin/util/exception.hpp +++ b/source/mijin/util/exception.hpp @@ -48,5 +48,30 @@ inline decltype(auto) ensure(TCondition&& condition, TExceptionArgs&&... args) } return std::forward(condition); } + +template +void walkExceptionCause(const std::exception_ptr& cause, TFunc func) +{ + if (cause) + { + try + { + std::rethrow_exception(cause); + } + catch(Exception& exc) + { + func(exc); + walkExceptionCause(exc.getCause(), std::move(func)); + } + catch(std::exception& exc) + { + func(exc); + } + catch(...) + { + func(nullptr); + } + } +} } // namespace mijin #endif // MIJIN_UTIL_EXCEPTION_HPP_INCLUDED