SERVER-126358: log attr in assert(expr, msg, attr) (#54299)

GitOrigin-RevId: 7e6cbaa620cb2880b92eadfc260d334e5a8c5a4b
This commit is contained in:
Steve McClure 2026-05-22 17:12:48 -04:00 committed by MongoDB Bot
parent a509e05dc1
commit b7fbac8dd4
3 changed files with 24 additions and 5 deletions

View File

@ -858,6 +858,27 @@ it("assertJsonFormat", function () {
);
});
function assertThrowsErrorWithAttr(assertFailureTriggerFn, {msg, attr}) {
try {
assertFailureTriggerFn();
} catch (e) {
assert.eq(msg, e.message, "unexpected error message");
assert.eq(toJsonForLog(attr), toJsonForLog(e.extraAttr), "unexpected extra attributes in plain mode");
return;
}
// Call the 'assertFailureTriggerFn' second time to make sure it actually throws.
assert.throws(assertFailureTriggerFn, [], "assertFailureTriggerFn");
}
it("assertPlainModeAttr", function () {
assertThrowsErrorWithAttr(
() => {
assert(false, "lorem ipsum", kAttr);
},
{msg: "assert failed : lorem ipsum", attr: {...kAttr}},
);
});
it("assertEqMessage", function () {
assertThrowsError(
() => {

View File

@ -178,7 +178,7 @@ function _doassert(msg, prefix, attr) {
}
doassert(_buildAssertionMessage(msg, prefix), attr);
}
doassert(_buildAssertionMessage(msg, formatErrorMsg(prefix, attr, tojson)), attr?.res);
doassert(_buildAssertionMessage(msg, formatErrorMsg(prefix, attr, tojson)), attr);
}
/**

View File

@ -31,10 +31,8 @@ function reconnect(db) {
function _getErrorWithCode(codeOrObj, message) {
let e = new Error(message);
if (typeof codeOrObj === "object" && codeOrObj !== null) {
if (TestData?.logFormat === "json") {
e.extraAttr = codeOrObj;
codeOrObj = codeOrObj.res ?? codeOrObj;
}
e.extraAttr = codeOrObj;
codeOrObj = codeOrObj.res ?? codeOrObj;
if (codeOrObj.hasOwnProperty("code")) {
e.code = codeOrObj.code;
}