From ba09e22c30b4e08e71252b1131afdd37dbcb9f92 Mon Sep 17 00:00:00 2001 From: Max Kellermann Date: Wed, 5 Jul 2017 17:02:21 +0200 Subject: [PATCH] util/Exception: add GetFullMessage(std::exception) --- src/util/Exception.cxx | 23 +++++++++++++++-------- src/util/Exception.hxx | 9 +++++++++ 2 files changed, 24 insertions(+), 8 deletions(-) diff --git a/src/util/Exception.cxx b/src/util/Exception.cxx index 0251f1c5c..c0e1856af 100644 --- a/src/util/Exception.cxx +++ b/src/util/Exception.cxx @@ -29,6 +29,20 @@ #include "Exception.hxx" +std::string +GetFullMessage(const std::exception &e, + const char *fallback, const char *separator) noexcept +{ + try { + std::rethrow_if_nested(e); + return e.what(); + } catch (...) { + return std::string(e.what()) + separator + + GetFullMessage(std::current_exception(), + fallback, separator); + } +} + std::string GetFullMessage(std::exception_ptr ep, const char *fallback, const char *separator) noexcept @@ -36,14 +50,7 @@ GetFullMessage(std::exception_ptr ep, try { std::rethrow_exception(ep); } catch (const std::exception &e) { - try { - std::rethrow_if_nested(e); - return e.what(); - } catch (...) { - return std::string(e.what()) + separator + - GetFullMessage(std::current_exception(), - fallback, separator); - } + return GetFullMessage(e, fallback, separator); } catch (const std::nested_exception &ne) { return GetFullMessage(ne.nested_ptr(), fallback, separator); } diff --git a/src/util/Exception.hxx b/src/util/Exception.hxx index 0ea24aa9b..b79796fdc 100644 --- a/src/util/Exception.hxx +++ b/src/util/Exception.hxx @@ -33,6 +33,15 @@ #include #include +/** + * Obtain the full concatenated message of an exception and its nested + * chain. + */ +std::string +GetFullMessage(const std::exception &e, + const char *fallback="Unknown exception", + const char *separator="; ") noexcept; + /** * Extract the full message of a C++ exception, considering its nested * exceptions (if any).