1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
|
#include <yt/yt/core/test_framework/framework.h>
#include <library/cpp/yt/error/error.h>
#include <library/cpp/yt/error/error_helpers.h>
#include <util/stream/str.h>
#include <util/string/join.h>
#include <util/string/split.h>
namespace NYT {
namespace {
using namespace NYson;
////////////////////////////////////////////////////////////////////////////////
class TAdlException
: public std::exception
{
public:
static int ResetCallCount()
{
return std::exchange(OverloadCallCount, 0);
}
const char* what() const noexcept override
{
return "Adl exception";
}
// Simulate overload from TAdlException::operator <<
template <class TLikeThis, class TArg>
requires std::derived_from<std::decay_t<TLikeThis>, TAdlException>
friend TLikeThis&& operator << (TLikeThis&& ex, const TArg& /*other*/)
{
++OverloadCallCount;
return std::forward<TLikeThis>(ex);
}
private:
static inline int OverloadCallCount = 0;
};
class TAdlArgument
{
public:
static int ResetCallCount()
{
return std::exchange(OverloadCallCount, 0);
}
// Simulate overload TAdlArgument::operator <<
friend TError operator << (TError&& error, const TAdlArgument& /*other*/)
{
static const TErrorAttribute Attr("attr", "attr_value");
++OverloadCallCount;
return std::move(error) << Attr;
}
friend TError operator << (const TError& error, const TAdlArgument& /*other*/)
{
static const TErrorAttribute Attr("attr", "attr_value");
++OverloadCallCount;
return error << Attr;
}
private:
static inline int OverloadCallCount = 0;
};
class TWidget
{
public:
TWidget()
{
DefaultConstructorCalls++;
};
TWidget(const TWidget&)
{
CopyConstructorCalls++;
}
TWidget& operator = (const TWidget&) = delete;
TWidget(TWidget&&)
{
MoveConstructorCalls++;
}
TWidget& operator = (TWidget&&) = delete;
static int ResetDefaultCount()
{
return std::exchange(DefaultConstructorCalls, 0);
}
static int ResetCopyCount()
{
return std::exchange(CopyConstructorCalls, 0);
}
static int ResetMoveCount()
{
return std::exchange(MoveConstructorCalls, 0);
}
private:
static inline int DefaultConstructorCalls = 0;
static inline int CopyConstructorCalls = 0;
static inline int MoveConstructorCalls = 0;
};
////////////////////////////////////////////////////////////////////////////////
template <class TOverloadTest, bool LeftOperandHasUserDefinedOverload = false>
void IterateTestOverEveryRightOperand(TOverloadTest& tester)
{
{
TErrorAttribute attribute("attr", "attr_value");
const auto& attributeRef = attribute;
tester(attributeRef);
}
{
std::vector<TErrorAttribute> attributeVector{{"attr1", "attr_value"}, {"attr2", "attr_value"}};
const auto& attributeVectorRef = attributeVector;
tester(attributeVectorRef);
}
{
TError error("Error");
const auto& errorRef = error;
tester(errorRef);
auto errorCopy = error;
tester(std::move(errorCopy));
if constexpr (!LeftOperandHasUserDefinedOverload) {
EXPECT_TRUE(errorCopy.IsOK());
}
}
{
std::vector<TError> vectorError{TError("Error"), TError("Error")};
const auto& vectorErrorRef = vectorError;
tester(vectorErrorRef);
auto vectorErrorCopy = vectorError;
tester(std::move(vectorErrorCopy));
if constexpr (!LeftOperandHasUserDefinedOverload) {
for (const auto& errorCopy : vectorErrorCopy) {
EXPECT_TRUE(errorCopy.IsOK());
}
}
}
{
TError error("Error");
const auto& attributeDictionaryRef = error.Attributes();
tester(attributeDictionaryRef);
}
{
try {
THROW_ERROR TError("Test error");
} catch(const NYT::TErrorException& ex) {
const auto& exRef = ex;
tester(exRef);
auto exCopy = ex;
tester(std::move(exCopy));
}
}
{
TErrorOr<int> err(std::exception{});
const auto& errRef = err;
tester(errRef);
auto errCopy = err;
tester(std::move(errCopy));
if constexpr (!LeftOperandHasUserDefinedOverload) {
EXPECT_TRUE(errCopy.IsOK());
}
}
{
TAdlArgument adlArg;
const TAdlArgument& adlArgRef = adlArg;
tester(adlArgRef);
if constexpr (!LeftOperandHasUserDefinedOverload) {
EXPECT_EQ(TAdlArgument::ResetCallCount(), 1);
}
}
}
template <class T>
void SetErrorAttribute(TError* error, TString key, const T& value)
{
*error <<= TErrorAttribute(key, value);
}
////////////////////////////////////////////////////////////////////////////////
TEST(TErrorTest, BitshiftOverloadsExplicitLeftOperand)
{
// TError&& overload.
auto moveTester = [] (auto&& arg) {
TError error = TError("Test error");
TError moved = std::move(error) << std::forward<decltype(arg)>(arg);
EXPECT_TRUE(error.IsOK());
EXPECT_EQ(moved.GetMessage(), "Test error");
};
IterateTestOverEveryRightOperand(moveTester);
// const TError& overloads.
auto copyTester = [] (auto&& arg) {
TError error = TError("Test error");
TError copy = error << std::forward<decltype(arg)>(arg);
EXPECT_EQ(error.GetMessage(), copy.GetMessage());
};
IterateTestOverEveryRightOperand(copyTester);
// Test that TError pr value binds correctly and the call itself is unambiguous.
auto prvalueTester = [] (auto&& arg) {
TError error = TError("Test error") << std::forward<decltype(arg)>(arg);
EXPECT_EQ(error.GetMessage(), "Test error");
};
IterateTestOverEveryRightOperand(prvalueTester);
}
TEST(TErrorTest, BitshiftOverloadsImplicitLeftOperand)
{
// We want to be able to write THROW_ERROR ex
auto throwErrorTester1 = [] (auto&& arg) {
try {
try {
THROW_ERROR TError("Test error");
} catch(const NYT::TErrorException& ex) {
THROW_ERROR ex << std::forward<decltype(arg)>(arg);
}
} catch(const NYT::TErrorException& ex) {
TError error = ex;
EXPECT_EQ(error.GetMessage(), "Test error");
}
};
IterateTestOverEveryRightOperand(throwErrorTester1);
// We also want to be able to write THROW_ERROR TError(smth) without compiler errors
auto throwErrorTester2 = [] (auto&& arg) {
try {
try {
THROW_ERROR TError("Test error");
} catch(const NYT::TErrorException& ex) {
THROW_ERROR TError(ex) << std::forward<decltype(arg)>(arg);
}
} catch(const NYT::TErrorException& ex) {
TError error = ex;
EXPECT_EQ(error.GetMessage(), "Test error");
}
};
IterateTestOverEveryRightOperand(throwErrorTester2);
// Left operand ADL finds the user-defined overload over NYT one.
// In this case AdlException should find templated function
// specialization with perfect match for args over conversions.
auto adlResolutionTester = [] (auto&& arg) {
TAdlException ex;
auto result = ex << std::forward<decltype(arg)>(arg);
static_assert(std::same_as<TAdlException, std::decay_t<decltype(result)>>);
EXPECT_EQ(TAdlException::ResetCallCount(), 1);
};
IterateTestOverEveryRightOperand<
decltype(adlResolutionTester),
/*LeftOperandHasUserDefinedOverload*/ true>(adlResolutionTester);
// Make sure no ambiguous calls.
auto genericErrorOrTester = [] (auto&& arg) {
TErrorOr<int> err(std::exception{});
TError error = err << std::forward<decltype(arg)>(arg);
EXPECT_EQ(error.GetCode(), NYT::EErrorCode::Generic);
};
IterateTestOverEveryRightOperand(genericErrorOrTester);
}
TEST(TErrorTest, Wrap)
{
TError error("Error");
auto wrapped = error.Wrap("Wrapped error");
EXPECT_EQ(wrapped.GetCode(), NYT::EErrorCode::Generic);
EXPECT_EQ(wrapped.GetMessage(), "Wrapped error");
EXPECT_EQ(wrapped.InnerErrors().size(), 1u);
EXPECT_EQ(wrapped.InnerErrors()[0], error);
auto triviallyWrapped = error.Wrap();
EXPECT_EQ(triviallyWrapped, error);
}
TEST(TErrorTest, WrapRValue)
{
TError error("Error");
TError errorCopy = error;
auto wrapped = std::move(errorCopy).Wrap("Wrapped error");
EXPECT_TRUE(errorCopy.IsOK());
EXPECT_EQ(wrapped.GetCode(), NYT::EErrorCode::Generic);
EXPECT_EQ(wrapped.GetMessage(), "Wrapped error");
EXPECT_EQ(wrapped.InnerErrors().size(), 1u);
EXPECT_EQ(wrapped.InnerErrors()[0], error);
TError anotherErrorCopy = error;
auto trviallyWrapped = std::move(anotherErrorCopy).Wrap();
EXPECT_TRUE(anotherErrorCopy.IsOK());
EXPECT_EQ(trviallyWrapped, error);
}
TEST(TErrorTest, ThrowErrorExceptionIfFailedMacroJustWorks)
{
TError error;
EXPECT_NO_THROW(THROW_ERROR_EXCEPTION_IF_FAILED(error, "Outer error"));
error = TError("Real error");
TError errorCopy = error;
try {
THROW_ERROR_EXCEPTION_IF_FAILED(errorCopy, "Outer error");
} catch (const std::exception& ex) {
TError outerError(ex);
EXPECT_TRUE(errorCopy.IsOK());
EXPECT_EQ(outerError.GetMessage(), "Outer error");
EXPECT_EQ(outerError.InnerErrors().size(), 1u);
EXPECT_EQ(outerError.InnerErrors()[0], error);
}
}
TEST(TErrorTest, ThrowErrorExceptionIfFailedMacroExpression)
{
try {
THROW_ERROR_EXCEPTION_IF_FAILED(
TError("Inner error")
<< TErrorAttribute("attr", "attr_value"),
"Outer error");
} catch (const std::exception& ex) {
TError outerError(ex);
EXPECT_EQ(outerError.GetMessage(), "Outer error");
EXPECT_EQ(outerError.InnerErrors().size(), 1u);
EXPECT_EQ(outerError.InnerErrors()[0].GetMessage(), "Inner error");
EXPECT_EQ(outerError.InnerErrors()[0].Attributes().Get<TString>("attr"), "attr_value");
}
}
TEST(TErrorTest, ThrowErrorExceptionIfFailedMacroDontStealValue)
{
TErrorOr<TWidget> widget = TWidget();
EXPECT_TRUE(widget.IsOK());
EXPECT_EQ(TWidget::ResetDefaultCount(), 1);
EXPECT_EQ(TWidget::ResetCopyCount(), 0);
EXPECT_EQ(TWidget::ResetMoveCount(), 1);
EXPECT_NO_THROW(THROW_ERROR_EXCEPTION_IF_FAILED(widget));
EXPECT_TRUE(widget.IsOK());
EXPECT_NO_THROW(widget.ValueOrThrow());
EXPECT_EQ(TWidget::ResetDefaultCount(), 0);
EXPECT_EQ(TWidget::ResetCopyCount(), 0);
EXPECT_EQ(TWidget::ResetMoveCount(), 0);
}
TEST(TErrorTest, ThrowErrorExceptionIfFailedMacroDontDupeCalls)
{
EXPECT_NO_THROW(THROW_ERROR_EXCEPTION_IF_FAILED(TErrorOr<TWidget>(TWidget())));
EXPECT_EQ(TWidget::ResetDefaultCount(), 1);
EXPECT_EQ(TWidget::ResetCopyCount(), 0);
EXPECT_EQ(TWidget::ResetMoveCount(), 1);
}
TEST(TErrorTest, ErrorSkeletonStubImplementation)
{
TError error("foo");
EXPECT_THROW(error.GetSkeleton(), std::exception);
}
TEST(TErrorTest, FormatCtor)
{
// EXPECT_EQ("Some error %v", TError("Some error %v").GetMessage()); // No longer compiles due to static analysis.
EXPECT_EQ("Some error hello", TError("Some error %v", "hello").GetMessage());
}
TEST(TErrorTest, FindRecursive)
{
auto inner = TError("Inner")
<< TErrorAttribute("inner_attr", 42);
auto error = TError("Error")
<< inner
<< TErrorAttribute("attr", 8);
auto attr = FindAttribute<int>(error, "attr");
EXPECT_TRUE(attr);
EXPECT_EQ(*attr, 8);
EXPECT_FALSE(FindAttribute<int>(error, "inner_attr"));
auto innerAttr = FindAttributeRecursive<int>(error, "inner_attr");
EXPECT_TRUE(innerAttr);
EXPECT_EQ(*innerAttr, 42);
}
TEST(TErrorTest, TruncateSimple)
{
auto error = TError("Some error")
<< TErrorAttribute("my_attr", "Attr value")
<< TError("Inner error");
auto truncatedError = error.Truncate();
EXPECT_EQ(error.GetCode(), truncatedError.GetCode());
EXPECT_EQ(error.GetMessage(), truncatedError.GetMessage());
EXPECT_EQ(error.GetPid(), truncatedError.GetPid());
EXPECT_EQ(error.GetTid(), truncatedError.GetTid());
EXPECT_EQ(error.GetDatetime(), truncatedError.GetDatetime());
EXPECT_EQ(error.Attributes().Get<TString>("my_attr"), truncatedError.Attributes().Get<TString>("my_attr"));
EXPECT_EQ(error.InnerErrors().size(), truncatedError.InnerErrors().size());
EXPECT_EQ(error.InnerErrors()[0].GetMessage(), truncatedError.InnerErrors()[0].GetMessage());
}
TEST(TErrorTest, TruncateLarge)
{
auto error = TError("Some long long error")
<< TError("First inner error")
<< TError("Second inner error")
<< TError("Third inner error")
<< TError("Fourth inner error");
SetErrorAttribute(&error, "my_attr", "Some long long attr");
auto truncatedError = error.Truncate(/*maxInnerErrorCount*/ 3, /*stringLimit*/ 10);
EXPECT_EQ(error.GetCode(), truncatedError.GetCode());
EXPECT_EQ("Some long ...<message truncated>", truncatedError.GetMessage());
EXPECT_EQ("...<attribute truncated>...", truncatedError.Attributes().Get<TString>("my_attr"));
EXPECT_EQ(truncatedError.InnerErrors().size(), 3u);
EXPECT_EQ("First inne...<message truncated>", truncatedError.InnerErrors()[0].GetMessage());
EXPECT_EQ("Second inn...<message truncated>", truncatedError.InnerErrors()[1].GetMessage());
EXPECT_EQ("Fourth inn...<message truncated>", truncatedError.InnerErrors()[2].GetMessage());
}
TEST(TErrorTest, TruncateSimpleRValue)
{
auto error = TError("Some error")
<< TErrorAttribute("my_attr", "Attr value")
<< TError("Inner error");
auto errorCopy = error;
auto truncatedError = std::move(errorCopy).Truncate();
EXPECT_TRUE(errorCopy.IsOK());
EXPECT_EQ(error.GetCode(), truncatedError.GetCode());
EXPECT_EQ(error.GetMessage(), truncatedError.GetMessage());
EXPECT_EQ(error.GetPid(), truncatedError.GetPid());
EXPECT_EQ(error.GetTid(), truncatedError.GetTid());
EXPECT_EQ(error.GetDatetime(), truncatedError.GetDatetime());
EXPECT_EQ(error.Attributes().Get<TString>("my_attr"), truncatedError.Attributes().Get<TString>("my_attr"));
EXPECT_EQ(error.InnerErrors().size(), truncatedError.InnerErrors().size());
EXPECT_EQ(error.InnerErrors()[0].GetMessage(), truncatedError.InnerErrors()[0].GetMessage());
}
TEST(TErrorTest, TruncateLargeRValue)
{
auto error = TError("Some long long error")
<< TError("First inner error")
<< TError("Second inner error")
<< TError("Third inner error")
<< TError("Fourth inner error");
SetErrorAttribute(&error, "my_attr", "Some long long attr");
auto errorCopy = error;
auto truncatedError = std::move(errorCopy).Truncate(/*maxInnerErrorCount*/ 3, /*stringLimit*/ 10);
EXPECT_TRUE(errorCopy.IsOK());
EXPECT_EQ(error.GetCode(), truncatedError.GetCode());
EXPECT_EQ("Some long ...<message truncated>", truncatedError.GetMessage());
EXPECT_EQ("...<attribute truncated>...", truncatedError.Attributes().Get<TString>("my_attr"));
EXPECT_EQ(truncatedError.InnerErrors().size(), 3u);
EXPECT_EQ("First inne...<message truncated>", truncatedError.InnerErrors()[0].GetMessage());
EXPECT_EQ("Second inn...<message truncated>", truncatedError.InnerErrors()[1].GetMessage());
EXPECT_EQ("Fourth inn...<message truncated>", truncatedError.InnerErrors()[2].GetMessage());
}
TEST(TErrorTest, TruncateConsistentOverloads)
{
auto error = TError("Some long long error")
<< TError("First inner error")
<< TError("Second inner error")
<< TError("Third inner error")
<< TError("Fourth inner error");
SetErrorAttribute(&error, "my_attr", "Some long long attr");
auto errorCopy = error;
auto truncatedRValueError = std::move(errorCopy).Truncate(/*maxInnerErrorCount*/ 3, /*stringLimit*/ 10);
auto trunactedLValueError = error.Truncate(/*maxInnerErrorCount*/ 3, /*stringLimit*/ 10);
EXPECT_EQ(truncatedRValueError, trunactedLValueError);
}
TEST(TErrorTest, TruncateWhitelist)
{
auto error = TError("Some error");
SetErrorAttribute(&error, "attr1", "Some long long attr");
SetErrorAttribute(&error, "attr2", "Some long long attr");
THashSet<TStringBuf> myWhitelist = {"attr2"};
auto truncatedError = error.Truncate(2, 10, myWhitelist);
EXPECT_EQ(error.GetCode(), truncatedError.GetCode());
EXPECT_EQ(error.GetMessage(), truncatedError.GetMessage());
EXPECT_EQ("...<attribute truncated>...", truncatedError.Attributes().Get<TString>("attr1"));
EXPECT_EQ("Some long long attr", truncatedError.Attributes().Get<TString>("attr2"));
}
TEST(TErrorTest, TruncateWhitelistRValue)
{
auto error = TError("Some error");
SetErrorAttribute(&error, "attr1", "Some long long attr");
SetErrorAttribute(&error, "attr2", "Some long long attr");
THashSet<TStringBuf> myWhitelist = {"attr2"};
auto errorCopy = error;
auto truncatedError = std::move(errorCopy).Truncate(2, 10, myWhitelist);
EXPECT_TRUE(errorCopy.IsOK());
EXPECT_EQ(error.GetCode(), truncatedError.GetCode());
EXPECT_EQ(error.GetMessage(), truncatedError.GetMessage());
EXPECT_EQ("...<attribute truncated>...", truncatedError.Attributes().Get<TString>("attr1"));
EXPECT_EQ("Some long long attr", truncatedError.Attributes().Get<TString>("attr2"));
}
TEST(TErrorTest, TruncateWhitelistInnerErrors)
{
auto innerError = TError("Inner error");
SetErrorAttribute(&innerError, "attr1", "Some long long attr");
SetErrorAttribute(&innerError, "attr2", "Some long long attr");
auto error = TError("Error") << innerError;
THashSet<TStringBuf> myWhitelist = {"attr2"};
auto truncatedError = error.Truncate(2, 15, myWhitelist);
EXPECT_EQ(truncatedError.InnerErrors().size(), 1u);
auto truncatedInnerError = truncatedError.InnerErrors()[0];
EXPECT_EQ(truncatedInnerError.GetCode(), innerError.GetCode());
EXPECT_EQ(truncatedInnerError.GetMessage(), innerError.GetMessage());
EXPECT_EQ("...<attribute truncated>...", truncatedInnerError.Attributes().Get<TString>("attr1"));
EXPECT_EQ("Some long long attr", truncatedInnerError.Attributes().Get<TString>("attr2"));
}
TEST(TErrorTest, TruncateWhitelistInnerErrorsRValue)
{
auto innerError = TError("Inner error");
SetErrorAttribute(&innerError, "attr1", "Some long long attr");
SetErrorAttribute(&innerError, "attr2", "Some long long attr");
auto error = TError("Error") << innerError;
THashSet<TStringBuf> myWhitelist = {"attr2"};
auto errorCopy = error;
auto truncatedError = std::move(errorCopy).Truncate(2, 15, myWhitelist);
EXPECT_TRUE(errorCopy.IsOK());
EXPECT_EQ(truncatedError.InnerErrors().size(), 1u);
auto truncatedInnerError = truncatedError.InnerErrors()[0];
EXPECT_EQ(truncatedInnerError.GetCode(), innerError.GetCode());
EXPECT_EQ(truncatedInnerError.GetMessage(), innerError.GetMessage());
EXPECT_EQ("...<attribute truncated>...", truncatedInnerError.Attributes().Get<TString>("attr1"));
EXPECT_EQ("Some long long attr", truncatedInnerError.Attributes().Get<TString>("attr2"));
}
TEST(TErrorTest, TruncateWhitelistSaveInnerError)
{
auto genericInner = TError("GenericInner");
auto whitelistedInner = TError("Inner")
<< TErrorAttribute("whitelisted_key", 42);
auto error = TError("Error")
<< (genericInner << TErrorAttribute("foo", "bar"))
<< whitelistedInner
<< genericInner;
error = std::move(error).Truncate(1, 20, {
"whitelisted_key"
});
EXPECT_TRUE(!error.IsOK());
EXPECT_EQ(error.InnerErrors().size(), 2u);
EXPECT_EQ(error.InnerErrors()[0], whitelistedInner);
EXPECT_EQ(error.InnerErrors()[1], genericInner);
// TODO: error_helpers???
EXPECT_TRUE(FindAttributeRecursive<int>(error, "whitelisted_key"));
EXPECT_FALSE(FindAttributeRecursive<int>(error, "foo"));
}
TEST(TErrorTest, YTExceptionToError)
{
try {
throw TSimpleException("message");
} catch (const std::exception& ex) {
TError error(ex);
EXPECT_EQ(NYT::EErrorCode::Generic, error.GetCode());
EXPECT_EQ("message", error.GetMessage());
}
}
TEST(TErrorTest, CompositeYTExceptionToError)
{
try {
try {
throw TSimpleException("inner message");
} catch (const std::exception& ex) {
throw TSimpleException(ex, "outer message");
}
} catch (const std::exception& ex) {
TError outerError(ex);
EXPECT_EQ(NYT::EErrorCode::Generic, outerError.GetCode());
EXPECT_EQ("outer message", outerError.GetMessage());
EXPECT_EQ(1, std::ssize(outerError.InnerErrors()));
const auto& innerError = outerError.InnerErrors()[0];
EXPECT_EQ(NYT::EErrorCode::Generic, innerError.GetCode());
EXPECT_EQ("inner message", innerError.GetMessage());
}
}
TEST(TErrorTest, YTExceptionWithAttributesToError)
{
try {
throw TSimpleException("message")
<< TExceptionAttribute{"Int64 value", static_cast<i64>(42)}
<< TExceptionAttribute{"double value", 7.77}
<< TExceptionAttribute{"bool value", false}
<< TExceptionAttribute{"String value", "FooBar"};
} catch (const std::exception& ex) {
TError error(ex);
EXPECT_EQ(NYT::EErrorCode::Generic, error.GetCode());
EXPECT_EQ("message", error.GetMessage());
auto i64value = error.Attributes().Find<i64>("Int64 value");
EXPECT_TRUE(i64value);
EXPECT_EQ(*i64value, static_cast<i64>(42));
auto doubleValue = error.Attributes().Find<double>("double value");
EXPECT_TRUE(doubleValue);
EXPECT_EQ(*doubleValue, 7.77);
auto boolValue = error.Attributes().Find<bool>("bool value");
EXPECT_TRUE(boolValue);
EXPECT_EQ(*boolValue, false);
auto stringValue = error.Attributes().Find<TString>("String value");
EXPECT_TRUE(stringValue);
EXPECT_EQ(*stringValue, "FooBar");
}
}
TEST(TErrorTest, AttributeSerialization)
{
auto getWeededText = [] (const TError& err) {
std::vector<TString> lines;
for (const auto& line : StringSplitter(ToString(err)).Split('\n')) {
if (!line.Contains("origin") && !line.Contains("datetime")) {
lines.push_back(TString{line});
}
}
return JoinSeq("\n", lines);
};
EXPECT_EQ(getWeededText(TError("E1") << TErrorAttribute("A1", "V1")), TString(
"E1\n"
" A1 V1\n"));
EXPECT_EQ(getWeededText(TError("E1") << TErrorAttribute("A1", "L1\nL2\nL3")), TString(
"E1\n"
" A1\n"
" L1\n"
" L2\n"
" L3\n"));
}
TEST(TErrorTest, MacroStaticAnalysis)
{
auto swallow = [] (auto expr) {
try {
expr();
} catch (...) {
}
};
swallow([] {
THROW_ERROR_EXCEPTION("Foo");
});
swallow([] {
THROW_ERROR_EXCEPTION("Hello, %v", "World");
});
swallow([] {
THROW_ERROR_EXCEPTION(NYT::EErrorCode::Generic, "Foo");
});
swallow([] {
THROW_ERROR_EXCEPTION(NYT::EErrorCode::Generic, "Foo%v", "Bar");
});
swallow([] {
THROW_ERROR_EXCEPTION(NYT::EErrorCode::Generic, "Foo%v%v", "Bar", "Baz");
});
swallow([] {
THROW_ERROR_EXCEPTION_IF_FAILED(TError{}, "Foo");
});
swallow([] {
THROW_ERROR_EXCEPTION_IF_FAILED(TError{}, "Foo%v", "Bar");
});
swallow([] {
THROW_ERROR_EXCEPTION_IF_FAILED(TError{}, "Foo%v%v", "Bar", "Baz");
});
swallow([] {
THROW_ERROR_EXCEPTION_IF_FAILED(TError{}, NYT::EErrorCode::Generic, "Foo%v", "Bar");
});
swallow([] {
THROW_ERROR_EXCEPTION_IF_FAILED(TError{}, NYT::EErrorCode::Generic, "Foo%v%v", "Bar", "Baz");
});
}
TEST(TErrorTest, WrapStaticAnalysis)
{
TError error;
Y_UNUSED(error.Wrap());
Y_UNUSED(error.Wrap(std::exception{}));
Y_UNUSED(error.Wrap("Hello"));
Y_UNUSED(error.Wrap("Hello, %v", "World"));
Y_UNUSED(error.Wrap(TRuntimeFormat{"Hello, %v"}));
}
// NB(arkady-e1ppa): Uncomment these occasionally to see
// that static analysis is still working.
TEST(TErrorTest, MacroStaticAnalysisBrokenFormat)
{
// auto swallow = [] (auto expr) {
// try {
// expr();
// } catch (...) {
// }
// };
// swallow([] {
// THROW_ERROR_EXCEPTION("Hello, %v");
// });
// swallow([] {
// THROW_ERROR_EXCEPTION(TErrorCode{}, "Foo%v");
// });
// swallow([] {
// THROW_ERROR_EXCEPTION_IF_FAILED(TError{}, "Foo%v");
// });
// swallow([] {
// THROW_ERROR_EXCEPTION_IF_FAILED(TError{}, TErrorCode{}, "Foo%v");
// });
}
////////////////////////////////////////////////////////////////////////////////
} // namespace
} // namespace NYT
|