diff options
author | David Bradford <david.bradford@mongodb.com> | 2018-07-12 09:13:24 -0400 |
---|---|---|
committer | David Bradford <david.bradford@mongodb.com> | 2018-07-12 09:43:07 -0400 |
commit | 8b95fa8d528069a8000926b5fd78d61336b25c17 (patch) | |
tree | 68cb5766a81de28f182b50199e046248c0bf1599 | |
parent | ca04f5bcf9bfa73c9162b3a77225c997c6deec8a (diff) | |
download | mongo-8b95fa8d528069a8000926b5fd78d61336b25c17.tar.gz |
SERVER-34788: Improve commandWorked/Failed assertion messages
-rw-r--r-- | jstests/noPassthrough/shell_cmd_assertions.js | 26 | ||||
-rw-r--r-- | src/mongo/shell/assert.js | 17 |
2 files changed, 35 insertions, 8 deletions
diff --git a/jstests/noPassthrough/shell_cmd_assertions.js b/jstests/noPassthrough/shell_cmd_assertions.js index 9c83339516f..76ed3c8c5a9 100644 --- a/jstests/noPassthrough/shell_cmd_assertions.js +++ b/jstests/noPassthrough/shell_cmd_assertions.js @@ -302,6 +302,32 @@ }); }); + tests.push(function invalidResponsesAttemptToProvideInformationToCommandWorks() { + const invalidResponses = [undefined, 'not a valid response', 42]; + + invalidResponses.forEach((invalidRes) => { + const error = assert.throws(() => { + assert.commandWorked(invalidRes); + }); + + assert.gte(error.message.indexOf(invalidRes), 0); + assert.gte(error.message.indexOf(typeof invalidRes), 0); + }); + }); + + tests.push(function invalidResponsesAttemptToProvideInformationCommandFailed() { + const invalidResponses = [undefined, 'not a valid response', 42]; + + invalidResponses.forEach((invalidRes) => { + const error = assert.throws(() => { + assert.commandFailed(invalidRes); + }); + + assert.gte(error.message.indexOf(invalidRes), 0); + assert.gte(error.message.indexOf(typeof invalidRes), 0); + }); + }); + tests.forEach((test) => { jsTest.log(`Starting test '${test.name}'`); setup(); diff --git a/src/mongo/shell/assert.js b/src/mongo/shell/assert.js index 36c37a8ee83..a3b0d0bb9c3 100644 --- a/src/mongo/shell/assert.js +++ b/src/mongo/shell/assert.js @@ -502,12 +502,16 @@ assert = (function() { res instanceof BulkWriteResult || res instanceof BulkWriteError; } - function _assertCommandWorked(res, msg, {ignoreWriteErrors, ignoreWriteConcernErrors}) { - _validateAssertionMessage(msg); - + function _validateCommandResponse(res, assertionName) { if (typeof res !== "object") { - doassert("unknown response given to commandWorked"); + doassert("unknown response type '" + typeof res + "' given to " + assertionName + + ", res='" + res + "'"); } + } + + function _assertCommandWorked(res, msg, {ignoreWriteErrors, ignoreWriteConcernErrors}) { + _validateAssertionMessage(msg); + _validateCommandResponse(res, "commandWorked"); // Keep this as a function so we don't call tojson if not necessary. const makeFailMsg = () => { @@ -547,10 +551,7 @@ assert = (function() { const kAnyErrorCode = Object.create(null); function _assertCommandFailed(res, expectedCode, msg) { _validateAssertionMessage(msg); - - if (typeof res !== "object") { - doassert("unknown response given to commandFailed"); - } + _validateCommandResponse(res, "commandFailed"); if (expectedCode !== kAnyErrorCode && !Array.isArray(expectedCode)) { expectedCode = [expectedCode]; |