aboutsummaryrefslogtreecommitdiffstats
path: root/yt/yql/plugin/native/error_helpers.cpp
blob: 8301c341f939b06733e749dae0b6f8f00810b680 (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
#include "error_helpers.h"

#include <library/cpp/yson/writer.h>

#include <ydb/library/yql/public/issue/yql_issue_id.h>

#include <ydb/library/yql/core/issue/protos/issue_id.pb.h>

namespace NYT::NYqlPlugin {

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

const int IssueToErrorCodesShift = 30000;

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

TString MessageToYtErrorYson(const TString& message)
{
    TStringStream yson;
    ::NYson::TYsonWriter writer(&yson);

    writer.OnBeginMap();
    writer.OnKeyedItem("code");
    writer.OnInt64Scalar(1); // Generic error
    writer.OnKeyedItem("message");
    writer.OnStringScalar(message);
    writer.OnKeyedItem("attributes");
    writer.OnBeginMap();
    writer.OnEndMap();

    writer.OnEndMap();

    return yson.Str();
}

TString IssuesToYtErrorYson(const NYql::TIssues& issues)
{
    TStringStream yson;
    ::NYson::TYsonWriter writer(&yson);

    auto serializePosition = [&] (const NYql::TPosition& position) {
        writer.OnBeginMap();
        {
            writer.OnKeyedItem("column");
            writer.OnInt64Scalar(position.Column);

            writer.OnKeyedItem("row");
            writer.OnInt64Scalar(position.Row);

            if (!position.File.empty()) {
                writer.OnKeyedItem("file");
                writer.OnStringScalar(position.File);
            }
        }
        writer.OnEndMap();
    };

    auto fn = [&] (const NYql::TIssue& issue, ui16 /*level*/) {
        writer.OnListItem();
        writer.OnBeginMap();
        {
            writer.OnKeyedItem("code");

            NYql::TIssueCode code = IssueToErrorCodesShift + issue.GetCode();
            writer.OnInt64Scalar(code);

            writer.OnKeyedItem("message");
            writer.OnStringScalar(issue.GetMessage());

            writer.OnKeyedItem("attributes");
            writer.OnBeginMap();
            {
                if (issue.Range().Position) {
                    writer.OnKeyedItem("start_position");
                    serializePosition(issue.Range().Position);
                }

                if (issue.Range().EndPosition) {
                    writer.OnKeyedItem("end_position");
                    serializePosition(issue.Range().EndPosition);
                }

                writer.OnKeyedItem("yql_status");
                writer.OnStringScalar(NYql::IssueCodeToString<NYql::TIssuesIds>(issue.GetCode()));

                writer.OnKeyedItem("severity");
                writer.OnStringScalar(SeverityToString(issue.GetSeverity()));
            }
            writer.OnEndMap();

            writer.OnKeyedItem("inner_errors");
            writer.OnBeginList();
        }
    };

    auto afterChildrenFn = [&] (const NYql::TIssue& /*issue*/, ui16 /*level*/) {
        writer.OnEndList();
        writer.OnEndMap();
    };

    writer.OnBeginMap();
    writer.OnKeyedItem("message");
    writer.OnStringScalar("There are some issues");
    writer.OnKeyedItem("code");
    writer.OnInt64Scalar(1);
    writer.OnKeyedItem("inner_errors");
    writer.OnBeginList();
    for (const auto& issue : issues) {
        WalkThroughIssues(issue, /*leafOnly*/ false, fn, afterChildrenFn);
    }
    writer.OnEndList();
    writer.OnKeyedItem("attributes");
    writer.OnBeginMap();
    writer.OnEndMap();
    writer.OnEndMap();
    return yson.Str();
}

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

} // namespace NYT::NYqlPLugin