diff options
author | David Bradford <david.bradford@mongodb.com> | 2018-03-20 12:08:40 -0400 |
---|---|---|
committer | David Bradford <david.bradford@mongodb.com> | 2018-03-20 12:08:40 -0400 |
commit | fd6f205a3ab17feec0cc9a0ba2a4baba8627f1f2 (patch) | |
tree | 0195b79ac9626c60300d81b79e171e6894e643dc /src/mongo/shell/assert.js | |
parent | 356d294b7afa0c665cdc580250df6859cb28d6fc (diff) | |
download | mongo-fd6f205a3ab17feec0cc9a0ba2a4baba8627f1f2.tar.gz |
SERVER-30879: check for write concern errors on assert.commandWorked
Diffstat (limited to 'src/mongo/shell/assert.js')
-rw-r--r-- | src/mongo/shell/assert.js | 45 |
1 files changed, 29 insertions, 16 deletions
diff --git a/src/mongo/shell/assert.js b/src/mongo/shell/assert.js index c1fea661cb5..ece2a4aed59 100644 --- a/src/mongo/shell/assert.js +++ b/src/mongo/shell/assert.js @@ -454,13 +454,17 @@ assert = (function() { assert.doesNotThrow(func, params, func.toString()); }; - function _rawReplyOkAndNoWriteErrors(raw) { + function _rawReplyOkAndNoWriteErrors(raw, {ignoreWriteErrors, ignoreWriteConcernErrors} = {}) { if (raw.ok === 0) { return false; } // A write command response may have ok:1 but write errors. - if (raw.hasOwnProperty("writeErrors") && raw.writeErrors.length > 0) { + if (!ignoreWriteErrors && raw.hasOwnProperty("writeErrors") && raw.writeErrors.length > 0) { + return false; + } + + if (!ignoreWriteConcernErrors && raw.hasOwnProperty("writeConcernError")) { return false; } @@ -474,7 +478,7 @@ assert = (function() { res instanceof BulkWriteResult || res instanceof BulkWriteError; } - function _assertCommandWorked(res, msg, {ignoreWriteErrors}) { + function _assertCommandWorked(res, msg, {ignoreWriteErrors, ignoreWriteConcernErrors}) { _validateAssertionMessage(msg); if (typeof res !== "object") { @@ -489,7 +493,7 @@ assert = (function() { if (_isWriteResultType(res)) { // These can only contain write errors, not command errors. if (!ignoreWriteErrors) { - assert.writeOK(res, msg); + assert.writeOK(res, msg, {ignoreWriteConcernErrors: ignoreWriteConcernErrors}); } } else if (res instanceof WriteCommandError || res instanceof Error) { // A WriteCommandError implies ok:0. @@ -499,14 +503,11 @@ assert = (function() { } else if (res.hasOwnProperty("ok")) { // Handle raw command responses or cases like MapReduceResult which extend command // response. - if (ignoreWriteErrors) { - if (res.ok === 0) { - doassert(makeFailMsg(), res); - } - } else { - if (!_rawReplyOkAndNoWriteErrors(res)) { - doassert(makeFailMsg(), res); - } + if (!_rawReplyOkAndNoWriteErrors(res, { + ignoreWriteErrors: ignoreWriteErrors, + ignoreWriteConcernErrors: ignoreWriteConcernErrors + })) { + doassert(makeFailMsg(), res); } } else if (res.hasOwnProperty("acknowledged")) { // CRUD api functions return plain js objects with an acknowledged property. @@ -563,13 +564,17 @@ assert = (function() { if (_rawReplyOkAndNoWriteErrors(res)) { doassert(makeFailMsg(), res); } + if (expectedCode !== kAnyErrorCode) { let foundCode = false; if (res.hasOwnProperty("code") && expectedCode.includes(res.code)) { foundCode = true; } else if (res.hasOwnProperty("writeErrors")) { foundCode = res.writeErrors.some((err) => expectedCode.includes(err.code)); + } else if (res.hasOwnProperty("writeConcernError")) { + foundCode = expectedCode.includes(res.writeConcernError.code); } + if (!foundCode) { doassert(makeFailCodeMsg(), res); } @@ -593,6 +598,15 @@ assert = (function() { return _assertCommandWorked(res, msg, {ignoreWriteErrors: true}); }; + assert.commandWorkedIgnoringWriteConcernErrors = function(res, msg) { + return _assertCommandWorked(res, msg, {ignoreWriteConcernErrors: true}); + }; + + assert.commandWorkedIgnoringWriteErrorsAndWriteConcernErrors = function(res, msg) { + return _assertCommandWorked( + res, msg, {ignoreWriteConcernErrors: true, ignoreWriteErrors: true}); + }; + assert.commandFailed = function(res, msg) { return _assertCommandFailed(res, kAnyErrorCode, msg); }; @@ -602,21 +616,20 @@ assert = (function() { return _assertCommandFailed(res, expectedCode, msg); }; - assert.writeOK = function(res, msg) { - + assert.writeOK = function(res, msg, {ignoreWriteConcernErrors} = {}) { var errMsg = null; if (res instanceof WriteResult) { if (res.hasWriteError()) { errMsg = "write failed with error: " + tojson(res); - } else if (res.hasWriteConcernError()) { + } else if (!ignoreWriteConcernErrors && res.hasWriteConcernError()) { errMsg = "write concern failed with errors: " + tojson(res); } } else if (res instanceof BulkWriteResult) { // Can only happen with bulk inserts if (res.hasWriteErrors()) { errMsg = "write failed with errors: " + tojson(res); - } else if (res.hasWriteConcernError()) { + } else if (!ignoreWriteConcernErrors && res.hasWriteConcernError()) { errMsg = "write concern failed with errors: " + tojson(res); } } else if (res instanceof WriteCommandError || res instanceof WriteError || |