summaryrefslogtreecommitdiff
path: root/jstests/libs/sbe_assert_error_override.js
blob: 233358e17433538585ce9e1c8495427712fd4319 (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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
/**
 * For a number of errors, the engine will raise different error codes when SBE mode is enabled vs.
 * disabled. When SBE mode is enabled, these differences in error codes can cause tests to fail that
 * otherwise would have passed.
 *
 * To expedite efforts to enable more tests under SBE mode, this file provides overrides for the
 * assert.commandFailedWithCode() and assert.writeErrorWithCode() APIs so that they treat certain
 * groups of error codes as being equivalent to each other. Note that these overrides also affect
 * how assertError(), assertErrorCode(), and assertErrCodeAndErrMsgContains() behave.
 *
 * Below the 'equivalentErrorCodesList' variable contains all known groups of error codes that
 * should be treated as equivalent to each other. As new groups of equivalent error codes are
 * discovered, they should be added to the list below.
 *
 * Note: This file should _only_ be included in a test if it has been observed that the test fails
 * due to differences in error codes when SBE mode is enabled.
 */
(function() {
"use strict";

// Below is the list of known equivalent error code groups. As new groups of equivalent error codes
// are discovered, they should be added to this list.
const equivalentErrorCodesList = [
    [9, 5166503, 5166605, 5338802],
    [16006, 4997703, 4998202],
    [16007, 5066300],
    [16020, 5066300],
    [16554, ErrorCodes.TypeMismatch, 4974201, 4974203],
    [16555, 5073102],
    [16608, 4848401],
    [16609, 5073101],
    [16610, 4848403],
    [16611, 5154000],
    [16612, 4974202],
    [16702, 5073001],
    [17041, 5159200],
    [28651, 5073201],
    [28664, 5153400],
    [28680, 4903701],
    [28689, 5126701],
    [28690, 5126702],
    [28691, 5126703],
    [28714, 4903710],
    [28761, 4903708],
    [
        28765,
        4903700,
        4903702,
        4903703,
        4903704,
        4903705,
        4903707,
        4903709,
        4822870,
        4995501,
        4995502
    ],
    [28766, 4903706],
    [31034, 4848972],
    [31095, 4848972],
    [34435, 5154901],
    [34443, 5154300],
    [34444, 5154303],
    [34445, 5154301],
    [34446, 5154304],
    [34447, 5154302],
    [34448, 5154305],
    [34449, 5154306],
    [40066, 4934200],
    [40085, 5155402],
    [40086, 5155400],
    [40087, 5155401],
    [40091, 5075300],
    [40092, 5075301, 5075302],
    [40093, 5075300],
    [40094, 5075301, 5075302],
    [40096, 5075303, 5075305],
    [40097, 5075304, 5075306],
    [40485, 4997704, 4998201, 5075307, 5166505, 5166602],
    [40515, 4848979],
    [40517, 4848980, 4997701, 5166504, 5166601],
    [40521, 4997702],
    [40522, 4997700],
    [40523, 4848972],
    [40533, 4998200],
    [50989, 4995503],
    [51044, 5688500],
    [51045, 5688500],
    [51104, 5073401],
    [51105, 5073405, 5126601],
    [51106, 5126603, 5126607],
    [51107, 5073406, 5126605],
    [51108, 5073401],
    [51109, 5073407, 5126602],
    [51110, 5073408, 5126604],
    [51111, 5073402],
    [51144, 5611301],
    [51151, 5126606],
    [51156, 5073403],
    [51246, 5291401],
    [51247, 5291402],
    [51744, 5154400],
    [51745, 5154400],
    [51746, 5154400],
    [5166306, 5166502],
    [5166307, 5166500, 5166501],
    [5166403, 5166603],
    [5166404, 5166604, 5439013],
    [5166405, 5166606],
    [5166502, 5439013],
    [5166503],
    [5338800, 5338801],
    [5338801, 5439015],
    [5338802, 5439016],
    [5687301, 5687400],
    [5687302, 5687401],
    [292, 5859000, 5843600, 5843601],
    [6045000, 5166606],
    [146, 13548],
    [ErrorCodes.TypeMismatch, 5156200, 5156201],
    [5439100, 40517, 7003907],
    [5439101, 40485, 7007908],
    [5439102, 5439012],
    [5439103, 5439013, 7003902],
    [5439104, 9, 7003903],
    [5439105, 5439017, 7003904, 7003905],
    [5439105, 5439018, 7003906],
    [5439106, 5439015, 7003909],
    [5439107, 5439016, 7003910],
];

// This map is generated based on the contents of 'equivalentErrorCodesList'. This map should _not_
// be modified. If you need to change which error codes are considered equivalent to each other, you
// should modify 'equivalentErrorCodesList' above.
const equivalentErrorCodesMap = function() {
    let mapOfSets = {};
    for (const arr of equivalentErrorCodesList) {
        for (const errorCode1 of arr) {
            if (!mapOfSets.hasOwnProperty(errorCode1)) {
                mapOfSets[errorCode1] = new Set();
            }

            for (const errorCode2 of arr) {
                if (errorCode1 != errorCode2) {
                    mapOfSets[errorCode1].add(errorCode2);
                }
            }
        }
    }

    let mapOfLists = {};
    for (const errorCode1 in mapOfSets) {
        let arr = [];
        for (const errorCode2 of mapOfSets[errorCode1]) {
            arr.push(errorCode2);
        }
        mapOfLists[errorCode1] = arr;
    }

    return mapOfLists;
}();

const lookupEquivalentErrorCodes = function(errorCodes) {
    if (errorCodes === assert._kAnyErrorCode) {
        return errorCodes;
    }

    if (!Array.isArray(errorCodes)) {
        errorCodes = [errorCodes];
    }

    let result = [];
    for (const errorCode1 of errorCodes) {
        result.push(errorCode1);
        if (equivalentErrorCodesMap.hasOwnProperty(errorCode1)) {
            for (const errorCode2 of equivalentErrorCodesMap[errorCode1]) {
                result.push(errorCode2);
            }
        }
    }

    return result;
};

// Override the assert.commandFailedWithCode() function.
const assertCommandFailedWithCodeOriginal = assert.commandFailedWithCode;
assert.commandFailedWithCode = function(res, expectedCode, msg) {
    return assertCommandFailedWithCodeOriginal(res, lookupEquivalentErrorCodes(expectedCode), msg);
};

// Override the assert.writeErrorWithCode() function.
const assertWriteErrorWithCodeOriginal = assert.writeErrorWithCode;
assert.writeErrorWithCode = function(res, expectedCode, msg) {
    return assertWriteErrorWithCodeOriginal(res, lookupEquivalentErrorCodes(expectedCode), msg);
};

// Override the assert.throwsWithCode() function.
const assertThrowsWithCodeOriginal = assert.throwsWithCode;
assert.throwsWithCode = function(func, expectedCode, params, msg) {
    return assertThrowsWithCodeOriginal(
        func, lookupEquivalentErrorCodes(expectedCode), params, msg);
};

// NOTE: Consider using 'assert.commandFailedWithCode' and 'assert.writeErrorWithCode' instead.
// This function should be only used when error code does not come from command. For instance, when
// validating explain output, which includes error code in the execution stats.
assert.errorCodeEq = function(actualErrorCode, expectedErrorCodes, msg) {
    if (expectedErrorCodes === assert._kAnyErrorCode) {
        return;
    }

    const equivalentErrorCodes = lookupEquivalentErrorCodes(expectedErrorCodes);
    assert(equivalentErrorCodes.includes(actualErrorCode),
           actualErrorCode + " not found in the list of expected error codes: " +
               tojson(equivalentErrorCodes) + ": " + msg);
};
}());