From fa10d601265343a64d9d5b7b32eb133010b24127 Mon Sep 17 00:00:00 2001 From: pechatnov Date: Tue, 6 May 2025 11:09:14 +0300 Subject: YT: Fix enricher, add from backtrace enricher MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit From backtrace enricher можно использовать для захвата бектрейсов при создании ошибок из исключений, а так же для перекладывания атрибутов из сложных исключений других библиотек. commit_hash:76711bd3bb7dbc1e41e43f80d43340d2ce8e4df7 --- library/cpp/yt/error/unittests/error_ut.cpp | 55 +++++++++++++++++++++++++++++ 1 file changed, 55 insertions(+) (limited to 'library/cpp/yt/error/unittests/error_ut.cpp') diff --git a/library/cpp/yt/error/unittests/error_ut.cpp b/library/cpp/yt/error/unittests/error_ut.cpp index a5576fad58a..33a8e8fa965 100644 --- a/library/cpp/yt/error/unittests/error_ut.cpp +++ b/library/cpp/yt/error/unittests/error_ut.cpp @@ -789,6 +789,61 @@ TEST(TErrorTest, MacroStaticAnalysisBrokenFormat) // }); } +TEST(TErrorTest, Enrichers) +{ + static auto getAttribute = [] (const TError& error) { + return error.Attributes().Get("test_attribute", ""); + }; + + { + static thread_local bool testEnricherEnabled = false; + testEnricherEnabled = true; + + TError::RegisterEnricher([](TError* error) { + if (testEnricherEnabled) { + *error <<= TErrorAttribute("test_attribute", getAttribute(*error) + "X"); + } + }); + + // Not from exception. + EXPECT_EQ(getAttribute(TError("E")), "X"); + EXPECT_EQ(getAttribute(TError(NYT::EErrorCode::Generic, "E")), "X"); + + // std::exception. + EXPECT_EQ(getAttribute(TError(std::runtime_error("E"))), "X"); + + // Copying. + EXPECT_EQ(getAttribute(TError(TError(std::runtime_error("E")))), "X"); + EXPECT_EQ(getAttribute(TError(TErrorException() <<= TError(std::runtime_error("E")))), "X"); + + testEnricherEnabled = false; + } + + { + static thread_local bool testFromExceptionEnricherEnabled = false; + testFromExceptionEnricherEnabled = true; + + TError::RegisterFromExceptionEnricher([](TError* error, const std::exception&) { + if (testFromExceptionEnricherEnabled) { + *error <<= TErrorAttribute("test_attribute", getAttribute(*error) + "X"); + } + }); + + // Not from exception. + EXPECT_EQ(getAttribute(TError("E")), ""); + EXPECT_EQ(getAttribute(TError(NYT::EErrorCode::Generic, "E")), ""); + + // From exception. + EXPECT_EQ(getAttribute(TError(std::runtime_error("E"))), "X"); + EXPECT_EQ(getAttribute(TError(TError(std::runtime_error("E")))), "X"); + + // From exception twice. + EXPECT_EQ(getAttribute(TError(TErrorException() <<= TError(std::runtime_error("E")))), "XX"); + + testFromExceptionEnricherEnabled = false; + } +} + //////////////////////////////////////////////////////////////////////////////// } // namespace -- cgit v1.3