summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--jstests/noPassthrough/shell_cmd_assertions.js11
-rw-r--r--src/mongo/shell/assert.js8
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);
}
};