aboutsummaryrefslogtreecommitdiffstats
path: root/yt/yt/core/misc/stripped_error.h
blob: fc4de80db60f4f9c9a10d11329e9c3b3d9f54ead (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
#pragma once

#include "public.h"

#include <yt/yt/core/ytree/public.h>

#include <library/cpp/yt/threading/public.h>

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

#include <library/cpp/yt/yson/public.h>

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

#include <library/cpp/yt/misc/property.h>

#include <util/system/compiler.h>
#include <util/system/getpid.h>

#include <util/generic/size_literals.h>

#include <type_traits>

namespace NYT {

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

//! An opaque wrapper around |int| value capable of conversions from |int|s and
//! arbitrary enum types.
class TErrorCode
{
public:
    using TUnderlying = int;

    constexpr TErrorCode();
    explicit constexpr TErrorCode(int value);
    template <class E>
        requires std::is_enum_v<E>
    constexpr TErrorCode(E value);

    constexpr operator int() const;

    template <class E>
        requires std::is_enum_v<E>
    constexpr operator E() const;

    template <class E>
        requires std::is_enum_v<E>
    constexpr bool operator == (E rhs) const;

    constexpr bool operator == (TErrorCode rhs) const;
private:
    int Value_;
};

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

void FormatValue(TStringBuilderBase* builder, TErrorCode code, TStringBuf spec);

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

struct TErrorAttribute
{
    template <class T>
    TErrorAttribute(const TString& key, const T& value)
        : Key(key)
        , Value(NYson::ConvertToYsonString(value))
    { }

    TErrorAttribute(const TString& key, const NYson::TYsonString& value)
        : Key(key)
        , Value(value)
    { }

    TString Key;
    NYson::TYsonString Value;
};

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

template <class TValue>
concept CErrorNestable = requires (TError& error, TValue&& operand)
{
    { error <<= std::forward<TValue>(operand) } -> std::same_as<TError&>;
};

template <>
class [[nodiscard]] TErrorOr<void>
{
public:
    TErrorOr();
    ~TErrorOr();

    TErrorOr(const TError& other);
    TErrorOr(TError&& other) noexcept;

    TErrorOr(const std::exception& ex);

    struct TDisableFormat
    { };
    static constexpr TDisableFormat DisableFormat = {};

    TErrorOr(TString message, TDisableFormat);
    TErrorOr(TErrorCode code, TString message, TDisableFormat);

    template <class... TArgs>
    explicit TErrorOr(
        TFormatString<TArgs...> format,
        TArgs&&... arg);

    template <class... TArgs>
    TErrorOr(
        TErrorCode code,
        TFormatString<TArgs...> format,
        TArgs&&... args);

    TError& operator = (const TError& other);
    TError& operator = (TError&& other) noexcept;

    static TError FromSystem();
    static TError FromSystem(int error);
    static TError FromSystem(const TSystemError& error);

    TErrorCode GetCode() const;
    TError& SetCode(TErrorCode code);

    TErrorCode GetNonTrivialCode() const;
    THashSet<TErrorCode> GetDistinctNonTrivialErrorCodes() const;

    const TString& GetMessage() const;
    TError& SetMessage(TString message);

    bool HasOriginAttributes() const;
    TProcessId GetPid() const;
    TStringBuf GetThreadName() const;
    NThreading::TThreadId GetTid() const;

    bool HasDatetime() const;
    TInstant GetDatetime() const;

    bool HasAttributes() const noexcept;

    const NYTree::IAttributeDictionary& Attributes() const;
    NYTree::IAttributeDictionary* MutableAttributes();

    const std::vector<TError>& InnerErrors() const;
    std::vector<TError>* MutableInnerErrors();

    // Used for deserialization only.
    TOriginAttributes* MutableOriginAttributes() const noexcept;
    void SetAttributes(NYTree::IAttributeDictionaryPtr attributes);

    TError Truncate(
        int maxInnerErrorCount = 2,
        i64 stringLimit = 16_KB,
        const THashSet<TStringBuf>& attributeWhitelist = {}) const &;
    TError Truncate(
        int maxInnerErrorCount = 2,
        i64 stringLimit = 16_KB,
        const THashSet<TStringBuf>& attributeWhitelist = {}) &&;

    bool IsOK() const;

    template <class U>
        requires (!CStringLiteral<std::remove_cvref_t<U>>)
    void ThrowOnError(U&& u) const &;
    template <class... TArgs>
    void ThrowOnError(TFormatString<TArgs...> format, TArgs&&... args) const &;
    template <class... TArgs>
    void ThrowOnError(TErrorCode code, TFormatString<TArgs...> format, TArgs&&... args) const &;
    inline void ThrowOnError() const &;

    template <class U>
        requires (!CStringLiteral<std::remove_cvref_t<U>>)
    void ThrowOnError(U&& u) &&;
    template <class... TArgs>
    void ThrowOnError(TFormatString<TArgs...> format, TArgs&&... args) &&;
    template <class... TArgs>
    void ThrowOnError(TErrorCode code, TFormatString<TArgs...> format, TArgs&&... args) &&;
    inline void ThrowOnError() &&;

    template <CInvocable<bool(const TError&)> TFilter>
    std::optional<TError> FindMatching(const TFilter& filter) const;
    template <CInvocable<bool(TErrorCode)> TFilter>
    std::optional<TError> FindMatching(const TFilter& filter) const;
    std::optional<TError> FindMatching(TErrorCode code) const;
    std::optional<TError> FindMatching(const THashSet<TErrorCode>& codes) const;

    template <class U>
        requires (!CStringLiteral<std::remove_cvref_t<U>>)
    TError Wrap(U&& u) const &;
    template <class... TArgs>
    TError Wrap(TFormatString<TArgs...> format, TArgs&&... args) const &;
    template <class... TArgs>
    TError Wrap(TErrorCode code, TFormatString<TArgs...> format, TArgs&&... args) const &;
    TError Wrap() const &;

    template <class U>
        requires (!CStringLiteral<std::remove_cvref_t<U>>)
    TError Wrap(U&& u) &&;
    template <class... TArgs>
    TError Wrap(TFormatString<TArgs...> format, TArgs&&... args) &&;
    template <class... TArgs>
    TError Wrap(TErrorCode code, TFormatString<TArgs...> format, TArgs&&... args) &&;
    TError Wrap() &&;

    //! Perform recursive aggregation of error codes and messages over the error tree.
    //! Result of this aggregation is suitable for error clustering in groups of
    //! "similar" errors. Refer to yt/yt/library/error_skeleton/skeleton_ut.cpp for examples.
    //!
    //! This method builds skeleton from scratch by doing complete error tree traversal,
    //! so calling it in computationally hot code is discouraged.
    //!
    //! In order to prevent core -> re2 dependency, implementation belongs to a separate library
    //! yt/yt/library/error_skeleton. Calling this method without PEERDIR'ing implementation
    //! results in an exception.
    TString GetSkeleton() const;

    TError& operator <<= (const TErrorAttribute& attribute) &;
    TError& operator <<= (const std::vector<TErrorAttribute>& attributes) &;
    TError& operator <<= (const TError& innerError) &;
    TError& operator <<= (TError&& innerError) &;
    TError& operator <<= (const std::vector<TError>& innerErrors) &;
    TError& operator <<= (std::vector<TError>&& innerErrors) &;
    TError& operator <<= (const NYTree::IAttributeDictionary& attributes) &;

    template <CErrorNestable TValue>
    TError&& operator << (TValue&& operand) &&;

    template <CErrorNestable TValue>
    TError operator << (TValue&& operand) const &;

    template <CErrorNestable TValue>
    TError&& operator << (const std::optional<TValue>& rhs) &&;

    template <CErrorNestable TValue>
    TError operator << (const std::optional<TValue>& rhs) const &;

private:
    class TImpl;
    std::unique_ptr<TImpl> Impl_;

    explicit TErrorOr(std::unique_ptr<TImpl> impl);

    void MakeMutable();
};

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

bool operator == (const TError& lhs, const TError& rhs);

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

void FormatValue(TStringBuilderBase* builder, const TError& error, TStringBuf spec);

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

using TErrorVisitor = std::function<void(const TError&, int depth)>;

//! Traverses the error tree in DFS order.
void TraverseError(
    const TError& error,
    const TErrorVisitor& visitor,
    int depth = 0);

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

template <class T>
struct TErrorTraits
{
    using TWrapped = TErrorOr<T>;
    using TUnwrapped = T;
};

template <class T>
struct TErrorTraits<TErrorOr<T>>
{
    using TUnderlying = T;
    using TWrapped = TErrorOr<T>;
    using TUnwrapped = T;
};

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

class TErrorException
    : public std::exception
{
public:
    DEFINE_BYREF_RW_PROPERTY(TError, Error);

public:
    TErrorException() = default;
    TErrorException(const TErrorException& other) = default;
    TErrorException(TErrorException&& other) = default;

    const char* what() const noexcept override;

private:
    mutable TString CachedWhat_;
};

// Make these templates to avoid type erasure during throw.
template <class TException>
    requires std::derived_from<std::remove_cvref_t<TException>, TErrorException>
TException&& operator <<= (TException&& ex, const TError& error);
template <class TException>
    requires std::derived_from<std::remove_cvref_t<TException>, TErrorException>
TException&& operator <<= (TException&& ex, TError&& error);

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

namespace NDetail {

struct TErrorAdaptor
{
    template <class TArg>
        requires std::constructible_from<TError, TArg>
    TError operator << (TArg&& rhs) const;

    template <class TArg>
        requires
            std::constructible_from<TError, TArg> &&
            std::derived_from<std::remove_cvref_t<TArg>, TError>
    TArg&& operator << (TArg&& rhs) const;
};

// Make these to correctly forward TError to Wrap call.
template <class TErrorLike, class U>
    requires
        std::derived_from<std::remove_cvref_t<TErrorLike>, TError> &&
        (!CStringLiteral<std::remove_cvref_t<U>>)
void ThrowErrorExceptionIfFailed(TErrorLike&& error, U&& u);

template <class TErrorLike, class... TArgs>
    requires std::derived_from<std::remove_cvref_t<TErrorLike>, TError>
void ThrowErrorExceptionIfFailed(TErrorLike&& error, TFormatString<TArgs...> format, TArgs&&... args);

template <class TErrorLike, class... TArgs>
    requires std::derived_from<std::remove_cvref_t<TErrorLike>, TError>
void ThrowErrorExceptionIfFailed(TErrorLike&& error, TErrorCode code, TFormatString<TArgs...> format, TArgs&&... args);

template <class TErrorLike>
    requires std::derived_from<std::remove_cvref_t<TErrorLike>, TError>
void ThrowErrorExceptionIfFailed(TErrorLike&& error);

} // namespace NDetail

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

#define THROW_ERROR \
    throw ::NYT::TErrorException() <<= ::NYT::NDetail::TErrorAdaptor() <<

#define THROW_ERROR_EXCEPTION(head, ...) \
    THROW_ERROR ::NYT::TError(head __VA_OPT__(,) __VA_ARGS__)

#define THROW_ERROR_EXCEPTION_IF_FAILED(error, ...) \
    ::NYT::NDetail::ThrowErrorExceptionIfFailed((error) __VA_OPT__(,) __VA_ARGS__); \

#define THROW_ERROR_EXCEPTION_UNLESS(condition, head, ...) \
    if ((condition)) {\
    } else { \
        THROW_ERROR ::NYT::TError(head __VA_OPT__(,) __VA_ARGS__); \
    }

#define THROW_ERROR_EXCEPTION_IF(condition, head, ...) \
    THROW_ERROR_EXCEPTION_UNLESS(!(condition), head, __VA_ARGS__)

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

template <class T>
class [[nodiscard]] TErrorOr
    : public TError
{
public:
    TErrorOr();

    TErrorOr(const T& value);
    TErrorOr(T&& value) noexcept;

    TErrorOr(const TErrorOr<T>& other);
    TErrorOr(TErrorOr<T>&& other) noexcept;

    TErrorOr(const TError& other);
    TErrorOr(TError&& other) noexcept;

    TErrorOr(const std::exception& ex);

    template <class U>
    TErrorOr(const TErrorOr<U>& other);
    template <class U>
    TErrorOr(TErrorOr<U>&& other) noexcept;

    TErrorOr<T>& operator = (const TErrorOr<T>& other)
        requires std::is_copy_assignable_v<T>;
    TErrorOr<T>& operator = (TErrorOr<T>&& other) noexcept
        requires std::is_nothrow_move_assignable_v<T>;

    const T& Value() const & Y_LIFETIME_BOUND;
    T& Value() & Y_LIFETIME_BOUND;
    T&& Value() && Y_LIFETIME_BOUND;

    template <class U>
        requires (!CStringLiteral<std::remove_cvref_t<U>>)
    const T& ValueOrThrow(U&& u) const & Y_LIFETIME_BOUND;
    template <class... TArgs>
    const T& ValueOrThrow(TFormatString<TArgs...> format, TArgs&&... args) const & Y_LIFETIME_BOUND;
    template <class... TArgs>
    const T& ValueOrThrow(TErrorCode code, TFormatString<TArgs...> format, TArgs&&... args) const & Y_LIFETIME_BOUND;
    const T& ValueOrThrow() const & Y_LIFETIME_BOUND;

    template <class U>
        requires (!CStringLiteral<std::remove_cvref_t<U>>)
    T& ValueOrThrow(U&& u) & Y_LIFETIME_BOUND;
    template <class... TArgs>
    T& ValueOrThrow(TFormatString<TArgs...> format, TArgs&&... args) & Y_LIFETIME_BOUND;
    template <class... TArgs>
    T& ValueOrThrow(TErrorCode code, TFormatString<TArgs...> format, TArgs&&... args) & Y_LIFETIME_BOUND;
    T& ValueOrThrow() & Y_LIFETIME_BOUND;

    template <class U>
        requires (!CStringLiteral<std::remove_cvref_t<U>>)
    T&& ValueOrThrow(U&& u) && Y_LIFETIME_BOUND;
    template <class... TArgs>
    T&& ValueOrThrow(TFormatString<TArgs...> format, TArgs&&... args) && Y_LIFETIME_BOUND;
    template <class... TArgs>
    T&& ValueOrThrow(TErrorCode code, TFormatString<TArgs...> format, TArgs&&... args) && Y_LIFETIME_BOUND;
    T&& ValueOrThrow() && Y_LIFETIME_BOUND;

    const T& ValueOrDefault(const T& defaultValue Y_LIFETIME_BOUND) const & Y_LIFETIME_BOUND;
    T& ValueOrDefault(T& defaultValue Y_LIFETIME_BOUND) & Y_LIFETIME_BOUND;
    constexpr T ValueOrDefault(T&& defaultValue) const &;
    constexpr T ValueOrDefault(T&& defaultValue) &&;

private:
    std::optional<T> Value_;
};

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

template <class T>
void FormatValue(TStringBuilderBase* builder, const TErrorOr<T>& error, TStringBuf spec);

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

template <class F, class... As>
auto RunNoExcept(F&& functor, As&&... args) noexcept -> decltype(functor(std::forward<As>(args)...))
{
    return functor(std::forward<As>(args)...);
}

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

} // namespace NYT

#define STRIPPED_ERROR_INL_H_
#include "stripped_error-inl.h"
#undef STRIPPED_ERROR_INL_H_