aboutsummaryrefslogtreecommitdiffstats
path: root/yql/essentials/providers/common/gateway/yql_provider_gateway.h
blob: d7981d0dddd305580907fd2a2bdbaafd2302e400 (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
#pragma once

#include <yql/essentials/core/issue/yql_issue.h>
#include <yql/essentials/public/issue/yql_issue_manager.h>

#include <library/cpp/threading/future/future.h>

namespace NYql {
namespace NCommon {

class TOperationResult {
public:
    [[nodiscard]]
    bool Success() const { return Status_ == TIssuesIds::SUCCESS; };
    const EYqlIssueCode& Status() const { return Status_; }
    const TIssues& Issues() const { return Issues_; }

    void SetSuccess() { Status_ = TIssuesIds::SUCCESS; }
    void SetStatus(EYqlIssueCode status) { Status_ = status; }

    void AddIssue(const TIssue& issue);
    void AddIssues(const TIssues& issues);
    void SetException(const std::exception& e, const TPosition& pos = {});

    void ReportIssues(TIssueManager& issueManager) const;
private:
    EYqlIssueCode Status_ = TIssuesIds::DEFAULT_ERROR;
    TIssues Issues_;
};

template<typename TResult>
TResult ResultFromException(const std::exception& e, const TPosition& pos = {}) {
    TResult result;
    result.SetException(e, pos);
    return result;
}

template<typename TResult>
TResult ResultFromIssues(EYqlIssueCode status, const TString& message, const TIssues& issues) {
    TIssue statusIssue = message.empty()
        ? YqlIssue(TPosition(), status)
        : YqlIssue(TPosition(), status, message);

    for (auto& issue : issues) {
        statusIssue.AddSubIssue(MakeIntrusive<TIssue>(issue));
    }

    TResult result;
    result.SetStatus(status);
    result.AddIssue(statusIssue);

    return result;
}

template<typename TResult>
TResult ResultFromIssues(EYqlIssueCode status, const TIssues& issues) {
    return ResultFromIssues<TResult>(status, "", issues);
}

template<typename TResult>
TResult ResultFromError(const TIssue& error) {
    TResult result;
    result.AddIssue(error);

    return result;
}

template<typename TResult>
TResult ResultFromError(const TIssues& error) {
    TResult result;
    result.AddIssues(error);

    return result;
}

template<typename TResult>
TResult ResultFromError(const TString& error, TPosition pos = TPosition()) {
    return ResultFromError<TResult>(TIssue(pos, error));
}

template<typename TResult>
TResult ResultFromErrors(const TIssues& errors) {
    TResult result;
    result.AddIssues(errors);
    return result;
}

template <typename T>
inline void SetPromiseValue(NThreading::TPromise<T>& promise, const NThreading::TFuture<T>& future)
{
    future.Subscribe([=] (const NThreading::TFuture<T>& f) mutable {
        try {
            promise.SetValue(f.GetValue());
        } catch (...) {
            promise.SetException(std::current_exception());
        }
    });
}

} // NCommon
} // NYql