diff options
author | svkrasnov <svkrasnov@yandex-team.ru> | 2022-03-23 14:49:38 +0300 |
---|---|---|
committer | svkrasnov <svkrasnov@yandex-team.ru> | 2022-03-23 14:49:38 +0300 |
commit | 0e6dafca9899ab0baf5ec5874ab205f0d8fa7f11 (patch) | |
tree | 5ef144b410bbc505a4dd07a3d1cc830d874bc606 /util/generic/yexception_ut.cpp | |
parent | ea82b3450005272f1a406cf1eac7a245c2e7c1ee (diff) | |
download | ydb-0e6dafca9899ab0baf5ec5874ab205f0d8fa7f11.tar.gz |
Introduce FormatCurrentException() based on TBackTrace::FromCurrentException()
ref:60218909c48b580eebc0b518f039afd46ca51713
Diffstat (limited to 'util/generic/yexception_ut.cpp')
-rw-r--r-- | util/generic/yexception_ut.cpp | 50 |
1 files changed, 50 insertions, 0 deletions
diff --git a/util/generic/yexception_ut.cpp b/util/generic/yexception_ut.cpp index cb3e29fed84..89869fcbe81 100644 --- a/util/generic/yexception_ut.cpp +++ b/util/generic/yexception_ut.cpp @@ -15,6 +15,7 @@ static inline void Throw2DontMove() { #include <util/random/mersenne.h> #include <util/stream/output.h> #include <util/string/subst.h> +#include <util/string/split.h> #include "yexception_ut.h" #include "bt_exception.h" @@ -48,6 +49,10 @@ class TExceptionTest: public TTestBase { UNIT_TEST(TestBackTrace) UNIT_TEST(TestEnsureWithBackTrace1) UNIT_TEST(TestEnsureWithBackTrace2) +#ifdef _YNDX_LIBUNWIND_ENABLE_EXCEPTION_BACKTRACE + UNIT_TEST(TestFormatCurrentException) + UNIT_TEST(TestFormatCurrentExceptionWithInvalidBacktraceFormatter) +#endif UNIT_TEST(TestRethrowAppend) UNIT_TEST(TestMacroOverload) UNIT_TEST(TestMessageCrop) @@ -128,6 +133,51 @@ private: UNIT_ASSERT(false); } + // TODO(svkrasnov): the output should be canonized after https://st.yandex-team.ru/YMAKE-103 +#ifdef _YNDX_LIBUNWIND_ENABLE_EXCEPTION_BACKTRACE + void TestFormatCurrentException() { + try { + throw std::logic_error("some exception"); // is instance of std::exception + UNIT_ASSERT(false); + } catch (...) { + TString exceptionMessage = FormatCurrentException(); + UNIT_ASSERT(exceptionMessage.Contains("(std::logic_error) some exception")); + TVector<TString> backtraceStrs = StringSplitter(exceptionMessage).Split('\n'); + UNIT_ASSERT(backtraceStrs.size() > 1); + } + } +#endif + +#ifdef _YNDX_LIBUNWIND_ENABLE_EXCEPTION_BACKTRACE + void TestFormatCurrentExceptionWithInvalidBacktraceFormatter() { + auto invalidFormatter = [](IOutputStream*, void* const*, size_t) { + Throw2DontMove(); + }; + SetFormatBackTraceFn(invalidFormatter); + + try { + Throw1DontMove(); + UNIT_ASSERT(false); + } catch (...) { + TString expected = "Caught:\n" + "(yexception) util/generic/yexception_ut.cpp:4: blabla\n\n" + "Failed to print backtrace: " + "(yexception) util/generic/yexception_ut.cpp:8: 1 qw 12.1"; + UNIT_ASSERT_EQUAL(FormatCurrentException(), expected); + } + try { + throw std::logic_error("std exception"); + UNIT_ASSERT(false); + } catch (...) { + TString expected = "Caught:\n" + "(std::logic_error) std exception\n\n" + "Failed to print backtrace: " + "(yexception) util/generic/yexception_ut.cpp:8: 1 qw 12.1"; + UNIT_ASSERT_EQUAL(FormatCurrentException(), expected); + } + } +#endif + inline void TestVirtualInheritance() { TStringStream ss; |