aboutsummaryrefslogtreecommitdiffstats
path: root/library/cpp/yt/exception/exception.h
blob: 2898b99f0de31260e0e63ab6317b01ebf9714cf2 (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
#pragma once

#include "attributes.h"

#include <util/generic/hash.h>

#include <exception>

namespace NYT {

////////////////////////////////////////////////////////////////////////////////
// This is poor man's version of NYT::TErrorException to be used in
// a limited subset of core libraries that are needed to implement NYT::TError.

class TSimpleException
    : public std::exception
{
public:
    using TAttributes = THashMap<
        TExceptionAttribute::TKey,
        TExceptionAttribute::TValue>;

    template <class TValue>
    static constexpr bool CNestable = requires (TSimpleException& ex, TValue&& operand) {
        { ex <<= std::forward<TValue>(operand) } -> std::same_as<TSimpleException&>;
    };

    explicit TSimpleException(TString message);
    TSimpleException(
        const std::exception& exception,
        TString message);

    const std::exception_ptr& GetInnerException() const;
    const char* what() const noexcept override;

    const TString& GetMessage() const;

    const TAttributes& GetAttributes() const &;
    TAttributes&& GetAttributes() &&;

    TSimpleException& operator<<= (TExceptionAttribute&& attribute) &;
    TSimpleException& operator<<= (std::vector<TExceptionAttribute>&& attributes) &;
    TSimpleException& operator<<= (TAttributes&& attributes) &;

    TSimpleException& operator<<= (const TExceptionAttribute& attribute) &;
    TSimpleException& operator<<= (const std::vector<TExceptionAttribute>& attributes) &;
    TSimpleException& operator<<= (const TAttributes& attributes) &;

    // NB: clang is incapable of parsing such requirements (which refer back to the class) out-of-line.
    // To keep this overload from winning in resolution
    // when constraint actually fails, we define method right here.
    template <class TValue>
        requires CNestable<TValue>
    TSimpleException&& operator<< (TValue&& value) &&
    {
        return std::move(*this <<= std::forward<TValue>(value));
    }

    template <class TValue>
        requires CNestable<TValue>
    TSimpleException operator<< (TValue&& value) const &
    {
        return TSimpleException(*this) << std::forward<TValue>(value);
    }

private:
    const std::exception_ptr InnerException_;
    const TString Message_;
    const TString What_;

    TAttributes Attributes_;
};

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

} // namespace NYT