SERVER-97715 Correctly propagate error code in assert.commandFailedWithCode (#29707)

GitOrigin-RevId: da8e52af5e61fef87741935d5edfb2e6485cfc24
This commit is contained in:
Ivan Fefer 2024-12-02 10:49:55 +01:00 committed by MongoDB Bot
parent a465797c8b
commit a7646b1aa7
4 changed files with 48 additions and 11 deletions

View File

@ -239,6 +239,7 @@ globals:
disablePrint: true
enablePrint: true
replSetMemberStatePrompt: true
hasErrorCode: true
helloStatePrompt: true
_validateMemberIndex: true
help: true

View File

@ -72,15 +72,7 @@ export const $config = (function() {
}
retryOnRetryableError(() => {
const res = db[collName].insert(docs);
if (res.writeErrors) {
for (let writeError of res.writeErrors) {
if (writeError.code == ErrorCodes.NoProgressMade) {
throw res;
}
}
}
TimeseriesTest.assertInsertWorked(res);
TimeseriesTest.assertInsertWorked(db[collName].insert(docs));
}, 100 /* numRetries */, undefined /* sleepMs */, [ErrorCodes.NoProgressMade]);
print(`Finished Inserting documents.`);

View File

@ -316,6 +316,50 @@ tests.push(function invalidResponsesAttemptToProvideInformationCommandFailed() {
});
});
tests.push(function assertsPreserveErrorCode() {
function runTestOnErrorResult(res, errorCode, test) {
let caught = false;
try {
test();
} catch (e) {
caught = true;
assert(hasErrorCode(e, [errorCode]),
"expect error code " + errorCode + " to be present in error " + tojson(e));
}
assert(caught);
}
function testErrorResult(res, errorCode) {
runTestOnErrorResult(res, errorCode, () => assert.commandWorked(res));
runTestOnErrorResult(
res, errorCode, () => assert.commandFailedWithCode(res, [errorCode + 1]));
runTestOnErrorResult(
res, errorCode, () => assert.commandWorkedOrFailedWithCode(res, [errorCode + 1]));
}
{
const commandNotFound = db.runCommand({"IHopeNobodyEverMakesThisACommand": 1});
testErrorResult(commandNotFound, ErrorCodes.CommandNotFound);
}
{
const rawCommandError = db.runCommand({insert: "coll", documents: [{_id: 1}]});
testErrorResult(rawCommandError, ErrorCodes.DuplicateKey);
}
{
const insertResultError = db.coll.insert({_id: 1});
testErrorResult(insertResultError, ErrorCodes.DuplicateKey);
}
{
const insertCommandError = db.coll.insert({x: 1}, {writeConcern: {"bad": 1}});
testErrorResult(insertCommandError, ErrorCodes.IDLUnknownField);
}
{
const bulkInsertError = db.coll.insert([{_id: 1}, {_id: 2}]);
testErrorResult(bulkInsertError, ErrorCodes.DuplicateKey);
}
});
tests.push(function assertCallsHangAnalyzer() {
function runAssertTest(f, expectCall) {
const oldMongoRunner = MongoRunner;

View File

@ -842,7 +842,7 @@ assert = (function() {
}
} else if (res.hasOwnProperty("acknowledged")) {
// CRUD api functions return plain js objects with an acknowledged property.
doassert(makeFailMsg());
doassert(makeFailMsg(), res);
} else {
doassert(_buildAssertionMessage(
msg, "unknown type of result, cannot check error: " + tojson(res)),
@ -978,7 +978,7 @@ assert = (function() {
if (errMsg) {
_runHangAnalyzerForSpecificFailureTypes(res);
doassert(_buildAssertionMessage(msg, errMsg));
doassert(_buildAssertionMessage(msg, errMsg), res);
}
return res;