From be273d4263da559563cb95b45d46e8e5c71d3148 Mon Sep 17 00:00:00 2001 From: Simon Rozman Date: Tue, 13 May 2025 09:36:22 +0200 Subject: [PATCH] exception: Add exception_msg helper Signed-off-by: Simon Rozman --- UnitTests/main.cpp | 2 +- include/stdex/exception.hpp | 17 +++++++++++++++++ 2 files changed, 18 insertions(+), 1 deletion(-) diff --git a/UnitTests/main.cpp b/UnitTests/main.cpp index cb9ad24a5..fd14807a3 100644 --- a/UnitTests/main.cpp +++ b/UnitTests/main.cpp @@ -39,7 +39,7 @@ int main(int, const char *[]) return 0; } catch (const std::exception& ex) { - std::cerr << ex.what() << " FAIL\n"; + std::cerr << stdex::exception_msg(ex) << " FAIL\n"; return 1; } } diff --git a/include/stdex/exception.hpp b/include/stdex/exception.hpp index 49258be92..288245fec 100644 --- a/include/stdex/exception.hpp +++ b/include/stdex/exception.hpp @@ -23,4 +23,21 @@ namespace stdex /// user_cancelled(_In_opt_z_ const char* msg = "operation cancelled") : runtime_error(msg) {} }; + + /// + /// Concatenates error messages when exception is nested. + /// + /// \param ex Exception + /// + /// \returns Concatenated exception messages with ": " delimiter. + /// + inline std::string exception_msg(const std::exception& ex) + { + try { std::rethrow_if_nested(ex); } + catch (const std::exception& nested_ex) { + return std::string(ex.what()) + ": " + exception_msg(nested_ex); + } + catch (...) {} + return ex.what(); + } }