diff options
author | Yu Jin Kang Park <yujin.kang@mongodb.com> | 2022-12-21 09:44:55 +0000 |
---|---|---|
committer | Evergreen Agent <no-reply@evergreen.mongodb.com> | 2022-12-21 10:14:59 +0000 |
commit | 1ad63b903d3a2be8ddc29072f2996863bde32ade (patch) | |
tree | eb95168d35b8e0946631c72c64cb372c4e4f61e5 | |
parent | f4d70be86e77725914eea3b2c538165e62d0fc1f (diff) | |
download | mongo-1ad63b903d3a2be8ddc29072f2996863bde32ade.tar.gz |
SERVER-71512 Fix assert.commandWorkedOrFailedWithCode
-rw-r--r-- | jstests/noPassthrough/shell_cmd_assertions.js | 11 | ||||
-rw-r--r-- | src/mongo/shell/assert.js | 8 |
2 files changed, 10 insertions, 9 deletions
diff --git a/jstests/noPassthrough/shell_cmd_assertions.js b/jstests/noPassthrough/shell_cmd_assertions.js index cfb5e4fceca..ccd864d0a38 100644 --- a/jstests/noPassthrough/shell_cmd_assertions.js +++ b/jstests/noPassthrough/shell_cmd_assertions.js @@ -94,8 +94,7 @@ tests.push(function rawCommandWriteOk() { assert.doesNotThrow(() => assert.commandWorkedIgnoringWriteErrors(res)); assert.throws(() => assert.commandFailed(res)); assert.throws(() => assert.commandFailedWithCode(res, 0)); - assert.doesNotThrow( - () => assert.commandWorkedOrFailedWithCode(res, 0, "threw even though succeeded")); + assert.doesNotThrow(() => assert.commandWorkedOrFailedWithCode(res, 0)); }); tests.push(function rawCommandWriteErr() { @@ -106,9 +105,9 @@ tests.push(function rawCommandWriteErr() { assert.doesNotThrow(() => assert.commandFailedWithCode(res, ErrorCodes.DuplicateKey)); assert.doesNotThrow( () => assert.commandFailedWithCode(res, [ErrorCodes.DuplicateKey, kFakeErrCode])); - assert.throws( + assert.doesNotThrow( () => assert.commandWorkedOrFailedWithCode( - res, [ErrorCodes.DuplicateKey, kFakeErrCode], "expected to throw on write error")); + res, [ErrorCodes.DuplicateKey, kFakeErrCode], "expected a write failure")); assert.throws(() => assert.commandWorkedOrFailedWithCode( res, [kFakeErrCode], "expected to throw on write error")); }); @@ -140,8 +139,8 @@ tests.push(function collMultiInsertWriteOk() { assert.doesNotThrow(() => assert.commandWorkedIgnoringWriteErrors(res)); assert.throws(() => assert.commandFailed(res)); assert.throws(() => assert.commandFailedWithCode(res, 0)); - assert.throws(() => - assert.commandWorkedOrFailedWithCode(res, 0, "threw even though succeeded")); + assert.doesNotThrow( + () => assert.commandWorkedOrFailedWithCode(res, 0, "threw even though succeeded")); }); tests.push(function collMultiInsertWriteErr() { diff --git a/src/mongo/shell/assert.js b/src/mongo/shell/assert.js index b893ec32616..681b25432b0 100644 --- a/src/mongo/shell/assert.js +++ b/src/mongo/shell/assert.js @@ -847,10 +847,12 @@ assert = (function() { assert.commandWorkedOrFailedWithCode = function commandWorkedOrFailedWithCode( res, errorCodeSet, msg) { - if (!res.ok) { - return assert.commandFailedWithCode(res, errorCodeSet, msg); - } else { + try { + // First check if the command worked. return assert.commandWorked(res, msg); + } catch (e) { + // If the command did not work, assert it failed with one of the specified codes. + return assert.commandFailedWithCode(res, errorCodeSet, msg); } }; |