aboutsummaryrefslogtreecommitdiffstats
path: root/library/cpp/yt/error/unittests/error_code_ut.cpp
diff options
context:
space:
mode:
authorarkady-e1ppa <arkady-e1ppa@yandex-team.com>2024-12-25 07:12:23 +0300
committerarkady-e1ppa <arkady-e1ppa@yandex-team.com>2024-12-25 07:31:55 +0300
commitb5dd91799751f9924acb7c17ddad16ddb2086bba (patch)
treef430b4e9f752af7411d5503bfcf823e9718056c2 /library/cpp/yt/error/unittests/error_code_ut.cpp
parent75f1af270a6cf9a17b65fde6d12efbb94f235960 (diff)
downloadydb-b5dd91799751f9924acb7c17ddad16ddb2086bba.tar.gz
YT-21233: Adjust (To/From)ErrorAttributeValue CPOs, revert ConvertTo becoming CPO, move TErrorCode and TError to library/cpp/yt/error
\[nodiff:caesar\] List of changes: 1) Make `ConvertTo` a normal function again (basically, a revert of previous change). It turned out to be very inconvenient entry point 2) Introduce concept for "Primitive types" which are handled without yt/core module. Please note that inclusion of core **does not** affect work with primitive types (otherwise we would be facing and ODR violation). These are numerics, duration, instant, string-like objects and guid 3) Introduce `FromErrorAttributeValue` which is a substitute to `ConvertTo` with special behavior (see below) 4) Scheme for (de-)serialization has changed: 1) Primitive types are handled via `ConvertToTextYsonString` and `ConvertFromTextYsonString` 2) Other types are not supported without yt/core. Otherwise `ConvertToYsonString(value, EYsonFormat::Text)` used for serialization and `ConvertTo<T>(value)` for deserialization. 5) New format of attribute value storage allows to not care about concrete type of the attribute (text yson strings can be detected since they start with `\"` and deserialized so are text yson bools, everything else is already human readable). This drops dependency on `TTokenizer` 6) Some small parts (e.g. enums or constants or aliases) of yt/core/misc files were moved to library/cpp/yt/\* locations and re-included to drop dependencies of stripped\_error on them 7) `TErrorAttributes` is now a simple hash map instead of a handle to `IAttributeDictionary` 8) `ExtractFromAttributes` weak symbol is finally moved to library/cpp/yt/error due to point 9 allowing to fully drop dependency on `IAttributeDictionary`. Combined with point 7 this drops dep on yt/core/ytree altogether 9) Moved `TErrorCode` to library/cpp/yt/error. There is a logger there which forces dep on library/cpp/yt/logging. It is not required really, and can be later removed 10) Moved `TError` with format and macroes to library/cpp/yt/error. It still (and probably forever will) depend on yson. What can be done next: Switch TYsonString to TString and move ConvertTo/FromTextYsonString to library/cpp/yt/error. This would remove dep on library/cpp/yt/yson\_string commit_hash:6f11dc478ab782a1f98a5aedcd45a4800d3f4e7b
Diffstat (limited to 'library/cpp/yt/error/unittests/error_code_ut.cpp')
-rw-r--r--library/cpp/yt/error/unittests/error_code_ut.cpp145
1 files changed, 145 insertions, 0 deletions
diff --git a/library/cpp/yt/error/unittests/error_code_ut.cpp b/library/cpp/yt/error/unittests/error_code_ut.cpp
new file mode 100644
index 0000000000..4bdb17f5d9
--- /dev/null
+++ b/library/cpp/yt/error/unittests/error_code_ut.cpp
@@ -0,0 +1,145 @@
+#include <yt/yt/core/test_framework/framework.h>
+
+#include <library/cpp/yt/error/error.h>
+#include <library/cpp/yt/error/error_code.h>
+
+#include <library/cpp/yt/string/format.h>
+
+#include <ostream>
+
+////////////////////////////////////////////////////////////////////////////////
+
+YT_DEFINE_ERROR_ENUM(
+ ((Global1) (-5))
+ ((Global2) (-6))
+);
+
+namespace NExternalWorld {
+
+////////////////////////////////////////////////////////////////////////////////
+
+YT_DEFINE_ERROR_ENUM(
+ ((X) (-11))
+ ((Y) (-22))
+ ((Z) (-33))
+);
+
+////////////////////////////////////////////////////////////////////////////////
+
+} // namespace NExternalWorld
+
+namespace NYT {
+
+void PrintTo(const TErrorCodeRegistry::TErrorCodeInfo& errorCodeInfo, std::ostream* os)
+{
+ *os << ToString(errorCodeInfo);
+}
+
+namespace NInternalLittleWorld {
+
+////////////////////////////////////////////////////////////////////////////////
+
+YT_DEFINE_ERROR_ENUM(
+ ((A) (-1))
+ ((B) (-2))
+ ((C) (-3))
+ ((D) (-4))
+);
+
+////////////////////////////////////////////////////////////////////////////////
+
+} // namespace NMyOwnLittleWorld
+
+namespace {
+
+////////////////////////////////////////////////////////////////////////////////
+
+YT_DEFINE_ERROR_ENUM(
+ ((Kek) (-57))
+ ((Haha) (-179))
+ ((Muahaha) (-1543))
+ ((Kukarek) (-2007))
+);
+
+TString TestErrorCodeFormatter(int code)
+{
+ return Format("formatted%v", code);
+}
+
+YT_DEFINE_ERROR_CODE_RANGE(-4399, -4200, "NYT::Test", TestErrorCodeFormatter);
+
+DEFINE_ENUM(EDifferentTestErrorCode,
+ ((ErrorNumberOne) (-10000))
+ ((ErrorNumberTwo) (-10001))
+ ((ErrorNumberThree) (-10002))
+);
+
+TString DifferentTestErrorCodeFormatter(int code)
+{
+ return TEnumTraits<EDifferentTestErrorCode>::ToString(static_cast<EDifferentTestErrorCode>(code));
+}
+
+YT_DEFINE_ERROR_CODE_RANGE(-10005, -10000, "NYT::DifferentTest", DifferentTestErrorCodeFormatter);
+
+TEST(TErrorCodeRegistryTest, Basic)
+{
+#ifdef _unix_
+ EXPECT_EQ(
+ TErrorCodeRegistry::Get()->Get(-1543),
+ (TErrorCodeRegistry::TErrorCodeInfo{"NYT::(anonymous namespace)", "Muahaha"}));
+#else
+ EXPECT_EQ(
+ TErrorCodeRegistry::Get()->Get(-1543),
+ (TErrorCodeRegistry::TErrorCodeInfo{"NYT::`anonymous namespace'", "Muahaha"}));
+#endif
+ EXPECT_EQ(
+ TErrorCodeRegistry::Get()->Get(-3),
+ (TErrorCodeRegistry::TErrorCodeInfo{"NYT::NInternalLittleWorld", "C"}));
+ EXPECT_EQ(
+ TErrorCodeRegistry::Get()->Get(-33),
+ (TErrorCodeRegistry::TErrorCodeInfo{"NExternalWorld", "Z"}));
+ EXPECT_EQ(
+ TErrorCodeRegistry::Get()->Get(-5),
+ (TErrorCodeRegistry::TErrorCodeInfo{"", "Global1"}));
+ EXPECT_EQ(
+ TErrorCodeRegistry::Get()->Get(-4300),
+ (TErrorCodeRegistry::TErrorCodeInfo{"NYT::Test", "formatted-4300"}));
+ EXPECT_EQ(
+ TErrorCodeRegistry::Get()->Get(-10002),
+ (TErrorCodeRegistry::TErrorCodeInfo{"NYT::DifferentTest", "ErrorNumberThree"}));
+ EXPECT_EQ(
+ TErrorCodeRegistry::Get()->Get(-10005),
+ (TErrorCodeRegistry::TErrorCodeInfo{"NYT::DifferentTest", "EDifferentTestErrorCode(-10005)"}));
+ EXPECT_EQ(
+ TErrorCodeRegistry::Get()->Get(-111),
+ (TErrorCodeRegistry::TErrorCodeInfo{"NUnknown", "ErrorCode-111"}));
+}
+
+DEFINE_ENUM(ETestEnumOne,
+ ((VariantOne) (0))
+ ((VariantTwo) (1))
+);
+
+DEFINE_ENUM(ETestEnumTwo,
+ ((DifferentVariantOne) (0))
+ ((DifferentVariantTwo) (1))
+);
+
+template <class T, class K>
+concept EquallyComparable = requires(T a, K b)
+{
+ { static_cast<T>(0) == static_cast<K>(0) };
+};
+
+TEST(TErrorCodeTest, ImplicitCastTest)
+{
+ // assert TErrorCode is in scope
+ using NYT::TErrorCode;
+ bool equallyComparable = EquallyComparable<ETestEnumOne, ETestEnumTwo>;
+ EXPECT_FALSE(equallyComparable);
+}
+
+////////////////////////////////////////////////////////////////////////////////
+
+} // namespace
+} // namespace NYT