summaryrefslogtreecommitdiffstats
path: root/yt/cpp/mapreduce/common/expected_error_guard.cpp
blob: dd2633834c7916c2cf27b214562304963929b5f3 (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
#include "expected_error_guard.h"

namespace NYT {

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

TExpectedErrorGuard::TExpectedErrorGuard(const TErrorPredicate& predicate)
    : PreviousPredicate_(std::exchange(GetCurrentPredicate(), predicate))
{ }

TExpectedErrorGuard::~TExpectedErrorGuard()
{
    Release();
}

bool TExpectedErrorGuard::IsErrorExpected(const TErrorResponse& error)
{
    auto predicate = GetCurrentPredicate();
    return predicate && predicate(error);
}

void TExpectedErrorGuard::Release()
{
    if (!Released_) {
        GetCurrentPredicate() = PreviousPredicate_;
        Released_ = true;
    }
}

TExpectedErrorGuard::TErrorPredicate& TExpectedErrorGuard::GetCurrentPredicate()
{
    thread_local TErrorPredicate CurrentPredicate;
    return CurrentPredicate;
}

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

} // namespace NYT