Coverage Report

Created: 2026-01-30 04:20

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/root/doris/be/src/common/status.cpp
Line
Count
Source
1
// Copyright (c) 2011 The LevelDB Authors. All rights reserved.
2
// Use of this source code is governed by a BSD-style license that can be
3
// found in the LICENSE file. See the AUTHORS file for names of contributors.
4
5
#include "common/status.h"
6
7
#include <gen_cpp/Status_types.h>
8
#include <gen_cpp/types.pb.h> // for PStatus
9
#include <rapidjson/encodings.h>
10
#include <rapidjson/prettywriter.h>
11
#include <rapidjson/stringbuffer.h>
12
13
#include <algorithm>
14
#include <new>
15
#include <vector>
16
17
#include "service/backend_options.h"
18
19
namespace doris {
20
namespace ErrorCode {
21
22
ErrorCodeState error_states[MAX_ERROR_CODE_DEFINE_NUM];
23
ErrorCodeInitializer error_code_init(10);
24
} // namespace ErrorCode
25
26
77.0k
void Status::to_thrift(TStatus* s) const {
27
77.0k
    s->error_msgs.clear();
28
77.0k
    if (ok()) {
29
75.0k
        s->status_code = TStatusCode::OK;
30
75.0k
        return;
31
75.0k
    }
32
    // Currently, there are many error codes, not defined in thrift and need pass to FE.
33
    // DCHECK(_code > 0)
34
    //        << "The error code has to > 0 because TStatusCode need it > 0, it's actual value is "
35
    //        << _code;
36
1.95k
    s->status_code = (int16_t)_code > 0 ? (TStatusCode::type)_code : TStatusCode::INTERNAL_ERROR;
37
38
1.95k
    if (_code == ErrorCode::VERSION_ALREADY_MERGED) {
39
0
        s->status_code = TStatusCode::OLAP_ERR_VERSION_ALREADY_MERGED;
40
1.95k
    } else if (_code == ErrorCode::TABLE_NOT_FOUND) {
41
0
        s->status_code = TStatusCode::TABLET_MISSING;
42
0
    }
43
44
1.95k
    s->error_msgs.push_back(fmt::format("({})[{}]{}", BackendOptions::get_localhost(),
45
18.4E
                                        code_as_string(), _err_msg ? _err_msg->_msg : ""));
46
1.95k
    s->__isset.error_msgs = true;
47
1.95k
}
48
49
55.4k
TStatus Status::to_thrift() const {
50
55.4k
    TStatus s;
51
55.4k
    to_thrift(&s);
52
55.4k
    return s;
53
55.4k
}
54
55
2.63M
void Status::to_protobuf(PStatus* s) const {
56
2.63M
    s->clear_error_msgs();
57
2.63M
    s->set_status_code((int)_code);
58
2.63M
    if (!ok()) {
59
6.87k
        s->add_error_msgs(fmt::format("({})[{}]{}", BackendOptions::get_localhost(),
60
18.4E
                                      code_as_string(), _err_msg ? _err_msg->_msg : ""));
61
6.87k
    }
62
2.63M
}
63
64
148
Status& Status::prepend(std::string_view msg) {
65
148
    if (!ok()) {
66
135
        if (_err_msg == nullptr) {
67
0
            _err_msg = std::make_unique<ErrMsg>();
68
0
        }
69
135
        _err_msg->_msg = std::string(msg) + _err_msg->_msg;
70
135
    }
71
148
    return *this;
72
148
}
73
74
5.20k
Status& Status::append(std::string_view msg) {
75
5.20k
    if (!ok()) {
76
5.19k
        if (_err_msg == nullptr) {
77
0
            _err_msg = std::make_unique<ErrMsg>();
78
0
        }
79
5.19k
        _err_msg->_msg.append(msg);
80
5.19k
    }
81
5.20k
    return *this;
82
5.20k
}
83
84
400
std::string Status::to_json() const {
85
400
    rapidjson::StringBuffer s;
86
400
    rapidjson::PrettyWriter<rapidjson::StringBuffer> writer(s);
87
400
    writer.StartObject();
88
    // status
89
400
    writer.Key("status");
90
400
    writer.String(code_as_string().c_str());
91
    // msg
92
400
    writer.Key("msg");
93
400
    ok() ? writer.String("OK") : writer.String(_err_msg ? _err_msg->_msg.c_str() : "");
94
400
    writer.EndObject();
95
400
    return s.GetString();
96
400
}
97
98
} // namespace doris