aboutsummaryrefslogtreecommitdiffstats
path: root/library/cpp/yt/exception/exception.cpp
blob: 71ece1ff2e0e650c353fcc2a0698f4c3206cec39 (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
#include "exception.h"

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

namespace NYT {

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

namespace {

template <class TRange>
void AddAttributes(TSimpleException::TAttributes& attrs, TRange&& range)
{
    for (auto&& [key, value] : range) {
        YT_VERIFY(attrs.emplace(std::move(key), std::move(value)).second);
    }
}

} // namespace

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

TSimpleException::TSimpleException(TString message)
    : Message_(std::move(message))
    , What_(Message_)
{ }

TSimpleException::TSimpleException(
    const std::exception& exception,
    TString message)
    : InnerException_(std::current_exception())
    , Message_(std::move(message))
    , What_(Message_ + "\n" + exception.what())
{ }

const std::exception_ptr& TSimpleException::GetInnerException() const
{
    return InnerException_;
}

const char* TSimpleException::what() const noexcept
{
    return What_.c_str();
}

const TString& TSimpleException::GetMessage() const
{
    return Message_;
}

const TSimpleException::TAttributes& TSimpleException::GetAttributes() const &
{
    return Attributes_;
}

TSimpleException::TAttributes&& TSimpleException::GetAttributes() &&
{
    return std::move(Attributes_);
}

TSimpleException& TSimpleException::operator<<= (TExceptionAttribute&& attribute) &
{
    YT_VERIFY(Attributes_.emplace(std::move(attribute.Key), std::move(attribute.Value)).second);
    return *this;
}

TSimpleException& TSimpleException::operator<<= (std::vector<TExceptionAttribute>&& attributes) &
{
    AddAttributes(Attributes_, std::move(attributes));
    return *this;
}

TSimpleException& TSimpleException::operator<<= (TAttributes&& attributes) &
{
    AddAttributes(Attributes_, std::move(attributes));
    return *this;
}

TSimpleException& TSimpleException::operator<<= (const TExceptionAttribute& attribute) &
{
    YT_VERIFY(Attributes_.emplace(attribute.Key, attribute.Value).second);
    return *this;
}

TSimpleException& TSimpleException::operator<<= (const std::vector<TExceptionAttribute>& attributes) &
{
    AddAttributes(Attributes_, attributes);
    return *this;
}

TSimpleException& TSimpleException::operator<<= (const TAttributes& attributes) &
{
    AddAttributes(Attributes_, attributes);
    return *this;
}

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

} // namespace NYT