aboutsummaryrefslogtreecommitdiffstats
path: root/library/cpp/yt/error/error.cpp
blob: 4790fbf606078010eea7ad147010357301300b8d (plain) (blame)
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
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
#include "error.h"

#include <library/cpp/yt/exception/exception.h>

#include <library/cpp/yt/error/error_attributes.h>
#include <library/cpp/yt/error/origin_attributes.h>

#include <library/cpp/yt/string/string.h>

#include <library/cpp/yt/system/proc.h>

#include <library/cpp/yt/yson_string/string.h>

#include <util/string/subst.h>

#include <util/system/error.h>
#include <util/system/thread.h>

namespace NYT {

////////////////////////////////////////////////////////////////////////////////

void FormatValue(TStringBuilderBase* builder, TErrorCode code, TStringBuf spec)
{
    FormatValue(builder, static_cast<int>(code), spec);
}

////////////////////////////////////////////////////////////////////////////////

constexpr TStringBuf ErrorMessageTruncatedSuffix = "...<message truncated>";

////////////////////////////////////////////////////////////////////////////////

class TError::TImpl
{
public:
    TImpl()
        : Code_(NYT::EErrorCode::OK)
    { }

    TImpl(const TError::TImpl& other)
        : Code_(other.Code_)
        , Message_(other.Message_)
        , OriginAttributes_(other.OriginAttributes_)
        , Attributes_(other.Attributes_)
        , InnerErrors_(other.InnerErrors_)
    { }

    explicit TImpl(TString message)
        : Code_(NYT::EErrorCode::Generic)
        , Message_(std::move(message))
    {
        OriginAttributes_.Capture();
    }

    TImpl(TErrorCode code, TString message)
        : Code_(code)
        , Message_(std::move(message))
    {
        if (!IsOK()) {
            OriginAttributes_.Capture();
        }
    }

    bool IsOK() const noexcept
    {
        return Code_ == NYT::EErrorCode::OK;
    }

    TErrorCode GetCode() const noexcept
    {
        return Code_;
    }

    void SetCode(TErrorCode code) noexcept
    {
        Code_ = code;
    }

    const TString& GetMessage() const noexcept
    {
        return Message_;
    }

    TString* MutableMessage() noexcept
    {
        return &Message_;
    }

    bool HasOriginAttributes() const noexcept
    {
        return OriginAttributes_.ThreadName.Length > 0;
    }

    const TOriginAttributes& OriginAttributes() const noexcept
    {
        return OriginAttributes_;
    }

    TOriginAttributes* MutableOriginAttributes() noexcept
    {
        return &OriginAttributes_;
    }

    TProcessId GetPid() const noexcept
    {
        return OriginAttributes_.Pid;
    }

    NThreading::TThreadId GetTid() const noexcept
    {
        return OriginAttributes_.Tid;
    }

    TStringBuf GetThreadName() const noexcept
    {
        return OriginAttributes_.ThreadName.ToStringBuf();
    }

    bool HasDatetime() const noexcept
    {
        return OriginAttributes_.Datetime != TInstant();
    }

    TInstant GetDatetime() const noexcept
    {
        return OriginAttributes_.Datetime;
    }

    void SetDatetime(TInstant datetime) noexcept
    {
        OriginAttributes_.Datetime = datetime;
    }

    bool HasAttributes() const noexcept
    {
        return true;
    }

    const TErrorAttributes& Attributes() const
    {
        return Attributes_;
    }

    void UpdateOriginAttributes()
    {
        OriginAttributes_ = NYT::NDetail::ExtractFromDictionary(&Attributes_);
    }

    TErrorAttributes* MutableAttributes() noexcept
    {
        return &Attributes_;
    }

    const std::vector<TError>& InnerErrors() const noexcept
    {
        return InnerErrors_;
    }

    std::vector<TError>* MutableInnerErrors() noexcept
    {
        return &InnerErrors_;
    }

private:
    TErrorCode Code_;
    TString Message_;

    TOriginAttributes OriginAttributes_{
        .Pid = 0,
        .Tid = NThreading::InvalidThreadId,
    };

    TErrorAttributes Attributes_;

    std::vector<TError> InnerErrors_;
};

////////////////////////////////////////////////////////////////////////////////

namespace {

bool IsWhitelisted(const TError& error, const THashSet<TStringBuf>& attributeWhitelist)
{
    for (const auto& key : error.Attributes().ListKeys()) {
        if (attributeWhitelist.contains(key)) {
            return true;
        }
    }

    for (const auto& innerError : error.InnerErrors()) {
        if (IsWhitelisted(innerError, attributeWhitelist)) {
            return true;
        }
    }

    return false;
}

//! Returns vector which consists of objects from errors such that:
//! if N is the number of objects in errors s.t. IsWhitelisted is true
//! then first N objects of returned vector are the ones for which IsWhitelisted is true
//! followed by std::max(0, maxInnerErrorCount - N - 1) remaining objects
//! finally followed by errors.back().
std::vector<TError>& ApplyWhitelist(std::vector<TError>& errors, const THashSet<TStringBuf>& attributeWhitelist, int maxInnerErrorCount)
{
    if (std::ssize(errors) < std::max(2, maxInnerErrorCount)) {
        return errors;
    }

    auto firstNotWhitelisted = std::partition(
        errors.begin(),
        std::prev(errors.end()),
        [&attributeWhitelist] (const TError& error) {
            return IsWhitelisted(error, attributeWhitelist);
        });

    int lastErrorOffset = std::max<int>(firstNotWhitelisted - errors.begin(), maxInnerErrorCount - 1);

    *(errors.begin() + lastErrorOffset) = std::move(errors.back());
    errors.resize(lastErrorOffset + 1);

    return errors;
}

} // namespace

////////////////////////////////////////////////////////////////////////////////

TError::TErrorOr() = default;

TError::~TErrorOr() = default;

TError::TErrorOr(const TError& other)
{
    if (!other.IsOK()) {
        Impl_ = std::make_unique<TImpl>(*other.Impl_);
    }
}

TError::TErrorOr(TError&& other) noexcept
    : Impl_(std::move(other.Impl_))
{ }

TError::TErrorOr(const TErrorException& errorEx) noexcept
{
    *this = errorEx.Error();
    // NB: TErrorException verifies that error not IsOK at throwing end.
}

TError::TErrorOr(const std::exception& ex)
{
    if (auto simpleException = dynamic_cast<const TSimpleException*>(&ex)) {
        *this = TError(NYT::EErrorCode::Generic, TRuntimeFormat{simpleException->GetMessage()});
        // NB: clang-14 is incapable of capturing structured binding variables
        //  so we force materialize them via this function call.
        auto addAttribute = [this] (const auto& key, const auto& value) {
            std::visit([&] (const auto& actual) {
                *this <<= TErrorAttribute(key, actual);
            }, value);
        };
        for (const auto& [key, value] : simpleException->GetAttributes()) {
            addAttribute(key, value);
        }
        try {
            if (simpleException->GetInnerException()) {
                std::rethrow_exception(simpleException->GetInnerException());
            }
        } catch (const std::exception& innerEx) {
            *this <<= TError(innerEx);
        }
    } else if (const auto* errorEx = dynamic_cast<const TErrorException*>(&ex)) {
        *this = errorEx->Error();
    } else {
        *this = TError(NYT::EErrorCode::Generic, TRuntimeFormat{ex.what()});
    }
    YT_VERIFY(!IsOK());
}

TError::TErrorOr(TString message, TDisableFormat)
    : Impl_(std::make_unique<TImpl>(std::move(message)))
{ }

TError::TErrorOr(TErrorCode code, TString message, TDisableFormat)
    : Impl_(std::make_unique<TImpl>(code, std::move(message)))
{ }

TError& TError::operator = (const TError& other)
{
    if (other.IsOK()) {
        Impl_.reset();
    } else {
        Impl_ = std::make_unique<TImpl>(*other.Impl_);
    }
    return *this;
}

TError& TError::operator = (TError&& other) noexcept
{
    Impl_ = std::move(other.Impl_);
    return *this;
}

TError TError::FromSystem()
{
    return FromSystem(LastSystemError());
}

TError TError::FromSystem(int error)
{
    return TError(TErrorCode(LinuxErrorCodeBase + error), TRuntimeFormat{LastSystemErrorText(error)}) <<
        TErrorAttribute("errno", error);
}

TError TError::FromSystem(const TSystemError& error)
{
    return FromSystem(error.Status());
}

TErrorCode TError::GetCode() const
{
    if (!Impl_) {
        return NYT::EErrorCode::OK;
    }
    return Impl_->GetCode();
}

TError& TError::SetCode(TErrorCode code)
{
    MakeMutable();
    Impl_->SetCode(code);
    return *this;
}

TErrorCode TError::GetNonTrivialCode() const
{
    if (!Impl_) {
        return NYT::EErrorCode::OK;
    }

    if (GetCode() != NYT::EErrorCode::Generic) {
        return GetCode();
    }

    for (const auto& innerError : InnerErrors()) {
        auto innerCode = innerError.GetNonTrivialCode();
        if (innerCode != NYT::EErrorCode::Generic) {
            return innerCode;
        }
    }

    return GetCode();
}

THashSet<TErrorCode> TError::GetDistinctNonTrivialErrorCodes() const
{
    THashSet<TErrorCode> result;
    TraverseError(*this, [&result] (const TError& error, int /*depth*/) {
        if (auto errorCode = error.GetCode(); errorCode != NYT::EErrorCode::OK) {
            result.insert(errorCode);
        }
    });
    return result;
}

const TString& TError::GetMessage() const
{
    if (!Impl_) {
        static const TString Result;
        return Result;
    }
    return Impl_->GetMessage();
}

TError& TError::SetMessage(TString message)
{
    MakeMutable();
    *Impl_->MutableMessage() = std::move(message);
    return *this;
}

bool TError::HasOriginAttributes() const
{
    if (!Impl_) {
        return false;
    }
    return Impl_->HasOriginAttributes();
}

bool TError::HasDatetime() const
{
    if (!Impl_) {
        return false;
    }
    return Impl_->HasDatetime();
}

TInstant TError::GetDatetime() const
{
    if (!Impl_) {
        return {};
    }
    return Impl_->GetDatetime();
}

TProcessId TError::GetPid() const
{
    if (!Impl_) {
        return 0;
    }
    return Impl_->GetPid();
}

NThreading::TThreadId TError::GetTid() const
{
    if (!Impl_) {
        return NThreading::InvalidThreadId;
    }
    return Impl_->GetTid();
}

TStringBuf TError::GetThreadName() const
{
    if (!Impl_) {
        static TString empty;
        return empty;
    }
    return Impl_->GetThreadName();
}

bool TError::HasAttributes() const noexcept
{
    if (!Impl_) {
        return false;
    }
    return Impl_->HasAttributes();
}

const TErrorAttributes& TError::Attributes() const
{
    if (!Impl_) {
        static TErrorAttributes empty;
        return empty;
    }
    return Impl_->Attributes();
}

TErrorAttributes* TError::MutableAttributes()
{
    MakeMutable();
    return Impl_->MutableAttributes();
}

const std::vector<TError>& TError::InnerErrors() const
{
    if (!Impl_) {
        static const std::vector<TError> Result;
        return Result;
    }
    return Impl_->InnerErrors();
}

std::vector<TError>* TError::MutableInnerErrors()
{
    MakeMutable();
    return Impl_->MutableInnerErrors();
}

TOriginAttributes* TError::MutableOriginAttributes() const noexcept
{
    if (!Impl_) {
        return nullptr;
    }
    return Impl_->MutableOriginAttributes();
}

void TError::UpdateOriginAttributes()
{
    if (!Impl_) {
        return;
    }
    Impl_->UpdateOriginAttributes();
}

const TString InnerErrorsTruncatedKey("inner_errors_truncated");

TError TError::Truncate(
    int maxInnerErrorCount,
    i64 stringLimit,
    const THashSet<TStringBuf>& attributeWhitelist) const &
{
    if (!Impl_) {
        return TError();
    }

    auto truncateInnerError = [=, &attributeWhitelist] (const TError& innerError) {
        return innerError.Truncate(maxInnerErrorCount, stringLimit, attributeWhitelist);
    };

    auto truncateAttributes = [stringLimit, &attributeWhitelist] (const TErrorAttributes& attributes, TErrorAttributes* mutableAttributes) {
        for (const auto& [key, value] : attributes.ListPairs()) {
            if (std::ssize(value) > stringLimit && !attributeWhitelist.contains(key)) {
                mutableAttributes->SetValue(
                    key,
                    NYT::ToErrorAttributeValue("...<attribute truncated>..."));
            } else {
                mutableAttributes->SetValue(
                    key,
                    value);
            }
        }
    };

    auto result = std::make_unique<TImpl>();
    result->SetCode(GetCode());
    *result->MutableMessage()
        = std::move(TruncateString(GetMessage(), stringLimit, ErrorMessageTruncatedSuffix));
    if (Impl_->HasAttributes()) {
        truncateAttributes(Impl_->Attributes(), result->MutableAttributes());
    }
    *result->MutableOriginAttributes() = Impl_->OriginAttributes();

    const auto& innerErrors = InnerErrors();
    auto& copiedInnerErrors = *result->MutableInnerErrors();

    if (std::ssize(innerErrors) <= maxInnerErrorCount) {
        for (const auto& innerError : innerErrors) {
            copiedInnerErrors.push_back(truncateInnerError(innerError));
        }
    } else {
        result->MutableAttributes()->SetValue(InnerErrorsTruncatedKey, NYT::ToErrorAttributeValue(true));

        // NB(arkady-e1ppa): We want to always keep the last inner error,
        // so we make room for it and do not check if it is whitelisted.
        for (int idx = 0; idx < std::ssize(innerErrors) - 1; ++idx) {
            const auto& innerError = innerErrors[idx];
            if (
                IsWhitelisted(innerError, attributeWhitelist) ||
                std::ssize(copiedInnerErrors) < maxInnerErrorCount - 1)
            {
                copiedInnerErrors.push_back(truncateInnerError(innerError));
            }
        }
        copiedInnerErrors.push_back(truncateInnerError(innerErrors.back()));
    }

    return TError(std::move(result));
}

TError TError::Truncate(
    int maxInnerErrorCount,
    i64 stringLimit,
    const THashSet<TStringBuf>& attributeWhitelist) &&
{
    if (!Impl_) {
        return TError();
    }

    auto truncateInnerError = [=, &attributeWhitelist] (TError& innerError) {
        innerError = std::move(innerError).Truncate(maxInnerErrorCount, stringLimit, attributeWhitelist);
    };

    auto truncateAttributes = [stringLimit, &attributeWhitelist] (TErrorAttributes* attributes) {
        for (const auto& [key, value] : attributes->ListPairs()) {
            if (std::ssize(value) > stringLimit && !attributeWhitelist.contains(key)) {
                attributes->SetValue(
                    key,
                    NYT::ToErrorAttributeValue("...<attribute truncated>..."));
            }
        }
    };

    TruncateStringInplace(Impl_->MutableMessage(), stringLimit, ErrorMessageTruncatedSuffix);
    if (Impl_->HasAttributes()) {
        truncateAttributes(Impl_->MutableAttributes());
    }
    if (std::ssize(InnerErrors()) <= maxInnerErrorCount) {
        for (auto& innerError : *MutableInnerErrors()) {
            truncateInnerError(innerError);
        }
    } else {
        auto& innerErrors = ApplyWhitelist(*MutableInnerErrors(), attributeWhitelist, maxInnerErrorCount);
        MutableAttributes()->SetValue(InnerErrorsTruncatedKey, NYT::ToErrorAttributeValue(true));

        for (auto& innerError : innerErrors) {
            truncateInnerError(innerError);
        }
    }

    return std::move(*this);
}

bool TError::IsOK() const
{
    if (!Impl_) {
        return true;
    }
    return Impl_->IsOK();
}

TError TError::Wrap() const &
{
    return *this;
}

TError TError::Wrap() &&
{
    return std::move(*this);
}

Y_WEAK TString GetErrorSkeleton(const TError& /*error*/)
{
    // Proper implementation resides in yt/yt/library/error_skeleton/skeleton.cpp.
    THROW_ERROR_EXCEPTION("Error skeleton implementation library is not linked; consider PEERDIR'ing yt/yt/library/error_skeleton");
}

TString TError::GetSkeleton() const
{
    return GetErrorSkeleton(*this);
}

std::optional<TError> TError::FindMatching(TErrorCode code) const
{
    return FindMatching([&] (TErrorCode errorCode) {
        return code == errorCode;
    });
}

std::optional<TError> TError::FindMatching(const THashSet<TErrorCode>& codes) const
{
    return FindMatching([&] (TErrorCode code) {
        return codes.contains(code);
    });
}

TError::TErrorOr(std::unique_ptr<TImpl> impl)
    : Impl_(std::move(impl))
{ }

void TError::MakeMutable()
{
    if (!Impl_) {
        Impl_ = std::make_unique<TImpl>();
    }
}

////////////////////////////////////////////////////////////////////////////////

TError& TError::operator <<= (const TErrorAttribute& attribute) &
{
    MutableAttributes()->SetAttribute(attribute);
    return *this;
}

TError& TError::operator <<= (const std::vector<TErrorAttribute>& attributes) &
{
    for (const auto& attribute : attributes) {
        MutableAttributes()->SetAttribute(attribute);
    }
    return *this;
}

TError& TError::operator <<= (const TError& innerError) &
{
    MutableInnerErrors()->push_back(innerError);
    return *this;
}

TError& TError::operator <<= (TError&& innerError) &
{
    MutableInnerErrors()->push_back(std::move(innerError));
    return *this;
}

TError& TError::operator <<= (const std::vector<TError>& innerErrors) &
{
    MutableInnerErrors()->insert(
        MutableInnerErrors()->end(),
        innerErrors.begin(),
        innerErrors.end());
    return *this;
}

TError& TError::operator <<= (std::vector<TError>&& innerErrors) &
{
    MutableInnerErrors()->insert(
        MutableInnerErrors()->end(),
        std::make_move_iterator(innerErrors.begin()),
        std::make_move_iterator(innerErrors.end()));
    return *this;
}

TError& TError::operator <<= (TAnyMergeableDictionaryRef attributes) &
{
    MutableAttributes()->MergeFrom(attributes);
    return *this;
}

////////////////////////////////////////////////////////////////////////////////

bool operator == (const TError& lhs, const TError& rhs)
{
    if (!lhs.MutableOriginAttributes() && !rhs.MutableOriginAttributes()) {
        return true;
    }
    // NB(arkady-e1ppa): Origin attributes equality comparison is
    // bit-wise but garbage bits are zeroed so it shouldn't matter.
    return
        lhs.GetCode() == rhs.GetCode() &&
        lhs.GetMessage() == rhs.GetMessage() &&
        *lhs.MutableOriginAttributes() == *rhs.MutableOriginAttributes() &&
        lhs.Attributes() == rhs.Attributes() &&
        lhs.InnerErrors() == rhs.InnerErrors();
}

////////////////////////////////////////////////////////////////////////////////

void AppendIndent(TStringBuilderBase* builer, int indent)
{
    builer->AppendChar(' ', indent);
}

void AppendAttribute(TStringBuilderBase* builder, const std::string& key, const std::string& value, int indent)
{
    AppendIndent(builder, indent + 4);
    if (value.find('\n') == std::string::npos) {
        builder->AppendFormat("%-15s %s", key, value);
    } else {
        builder->AppendString(key);
        std::string indentedValue = "\n" + value;
        SubstGlobal(indentedValue, "\n", "\n" + std::string(static_cast<size_t>(indent + 8), ' '));
        // Now first line in indentedValue is empty and every other line is indented by 8 spaces.
        builder->AppendString(indentedValue);
    }
    builder->AppendChar('\n');
}

void AppendError(TStringBuilderBase* builder, const TError& error, int indent)
{
    auto isStringTextYson = [] (TStringBuf str) {
        return
            str &&
            std::ssize(str) != 0 &&
            str.front() == '\"';
    };
    auto isBoolTextYson = [] (TStringBuf str) {
        return
            str == "%false" ||
            str == "%true";
    };

    if (error.IsOK()) {
        builder->AppendString("OK");
        return;
    }

    AppendIndent(builder, indent);
    builder->AppendString(error.GetMessage());
    builder->AppendChar('\n');

    if (error.GetCode() != NYT::EErrorCode::Generic) {
        AppendAttribute(builder, "code", NYT::ToString(static_cast<int>(error.GetCode())), indent);
    }

    // Pretty-print origin.
    const auto* originAttributes = error.MutableOriginAttributes();
    YT_ASSERT(originAttributes);
    if (error.HasOriginAttributes()) {
        AppendAttribute(
            builder,
            "origin",
            NYT::NDetail::FormatOrigin(*originAttributes),
            indent);
    } else if (IsErrorSanitizerEnabled() && originAttributes->Host.operator bool()) {
        AppendAttribute(
            builder,
            "host",
            TString{originAttributes->Host},
            indent);
    }

    if (error.HasDatetime()) {
        AppendAttribute(
            builder,
            "datetime",
            Format("%v", error.GetDatetime()),
            indent);
    }

    for (const auto& [key, value] : error.Attributes().ListPairs()) {
        if (isStringTextYson(value)) {
            AppendAttribute(builder, key, NDetail::ConvertFromTextYsonString<std::string>(value), indent);
        } else if (isBoolTextYson(value)) {
            AppendAttribute(builder, key, std::string(FormatBool(NDetail::ConvertFromTextYsonString<bool>(value))), indent);
        } else {
            AppendAttribute(builder, key, value, indent);
        }
    }

    for (const auto& innerError : error.InnerErrors()) {
        builder->AppendChar('\n');
        AppendError(builder, innerError, indent + 2);
    }
}

////////////////////////////////////////////////////////////////////////////////

void FormatValue(TStringBuilderBase* builder, const TError& error, TStringBuf /*spec*/)
{
    AppendError(builder, error, 0);
}

////////////////////////////////////////////////////////////////////////////////

void TraverseError(const TError& error, const TErrorVisitor& visitor, int depth)
{
    visitor(error, depth);
    for (const auto& inner : error.InnerErrors()) {
        TraverseError(inner, visitor, depth + 1);
    }
}

////////////////////////////////////////////////////////////////////////////////

const char* TErrorException::what() const noexcept
{
    if (CachedWhat_.empty()) {
        CachedWhat_ = ToString(Error_);
    }
    return CachedWhat_.data();
}

////////////////////////////////////////////////////////////////////////////////

} // namespace NYT