summaryrefslogtreecommitdiff
path: root/src/mongo/base/status.cpp
blob: b7f0b29302f96d6470d1ba65ada725ef5b6c1631 (plain)
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
/**
 *    Copyright (C) 2018-present MongoDB, Inc.
 *
 *    This program is free software: you can redistribute it and/or modify
 *    it under the terms of the Server Side Public License, version 1,
 *    as published by MongoDB, Inc.
 *
 *    This program is distributed in the hope that it will be useful,
 *    but WITHOUT ANY WARRANTY; without even the implied warranty of
 *    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *    Server Side Public License for more details.
 *
 *    You should have received a copy of the Server Side Public License
 *    along with this program. If not, see
 *    <http://www.mongodb.com/licensing/server-side-public-license>.
 *
 *    As a special exception, the copyright holders give permission to link the
 *    code of portions of this program with the OpenSSL library under certain
 *    conditions as described in each individual source file and distribute
 *    linked combinations including the program with the OpenSSL library. You
 *    must comply with the Server Side Public License in all respects for
 *    all of the code used other than as permitted herein. If you modify file(s)
 *    with this exception, you may extend this exception to your version of the
 *    file(s), but you are not obligated to do so. If you do not wish to do so,
 *    delete this exception statement from your version. If you delete this
 *    exception statement from all source files in the program, then also delete
 *    it in the license file.
 */


#include <ostream>
#include <sstream>

#include "mongo/base/status.h"
#include "mongo/db/jsobj.h"
#include "mongo/logv2/log.h"
#include "mongo/util/assert_util.h"
#include "mongo/util/str.h"

#define MONGO_LOGV2_DEFAULT_COMPONENT ::mongo::logv2::LogComponent::kControl


namespace mongo {

Status& Status::addContext(StringData reasonPrefix) {
    if (!isOK()) {
        boost::intrusive_ptr oldInfo = std::move(_error);
        const Status::ErrorInfo& e = *oldInfo;
        _error = _createErrorInfo(e.code, reasonPrefix + causedBy(e.reason), e.extra);
    }
    return *this;
}

std::string Status::toString() const {
    StringBuilder sb;
    sb << *this;
    return sb.str();
}

void Status::serialize(BSONObjBuilder* builder) const {
    builder->append("code", code());
    builder->append("codeName", ErrorCodes::errorString(code()));
    if (!isOK()) {
        builder->append("errmsg", reason());
        if (const auto& ei = extraInfo())
            ei->serialize(builder);
    }
}

boost::intrusive_ptr<const Status::ErrorInfo> Status::_createErrorInfo(
    ErrorCodes::Error code, std::string reason, std::shared_ptr<const ErrorExtraInfo> extra) {
    if (code == ErrorCodes::OK)
        return nullptr;
    if (extra) {
        // The public API prevents getting into this state.
        invariant(ErrorCodes::canHaveExtraInfo(code));
    } else if (ErrorCodes::mustHaveExtraInfo(code)) {
        // If an ErrorExtraInfo class is non-optional, return an error.

        // This is possible if code calls a 2-argument Status constructor with a code that should
        // have extra info.
        if (kDebugBuild) {
            // Make it easier to find this issue by fatally failing in debug builds.
            LOGV2_FATAL(40680, "Code {code} is supposed to have extra info", "code"_attr = code);
        }

        // In release builds, replace the error code. This maintains the invariant that all Statuses
        // for a code that is supposed to hold extra info hold correctly-typed extra info, without
        // crashing the server.
        return make_intrusive<ErrorInfo>(
            ErrorCodes::Error(40671),
            str::stream() << "Missing required extra info for error code " << code,
            std::move(extra));
    }
    return make_intrusive<ErrorInfo>(code, std::move(reason), std::move(extra));
}

boost::intrusive_ptr<const Status::ErrorInfo> Status::_parseErrorInfo(ErrorCodes::Error code,
                                                                      std::string reason,
                                                                      const BSONObj& extraObj) {
    std::shared_ptr<const ErrorExtraInfo> extra;
    if (auto parser = ErrorExtraInfo::parserFor(code)) {
        try {
            extra = parser(extraObj);
        } catch (const DBException& ex) {
            if (ErrorCodes::mustHaveExtraInfo(code)) {
                return ex.toStatus(str::stream() << "Error parsing extra info for " << code)._error;
            }
        }
    }
    return _createErrorInfo(code, std::move(reason), std::move(extra));
}

std::ostream& Status::_streamTo(std::ostream& os) const {
    return os << codeString() << " " << reason();
}

StringBuilder& Status::_streamTo(StringBuilder& sb) const {
    sb << codeString();
    if (!isOK()) {
        try {
            if (const auto& extra = extraInfo()) {
                BSONObjBuilder bob;
                extra->serialize(&bob);
                sb << bob.done();
            }
        } catch (const DBException&) {
            // This really shouldn't happen but it would be really annoying if it broke error
            // logging in production.
            if (kDebugBuild) {
                LOGV2_FATAL_CONTINUE(
                    23806,
                    "Error serializing extra info for {status_code} in Status::toString()",
                    "status_code"_attr = code());
                std::terminate();
            }
        }
        sb << ": " << reason();
    }
    return sb;
}

}  // namespace mongo