[SERVER-71512] assert.commandWorkedOrFailedWithCode unexpected behaviour with some commands Created: 21/Nov/22  Updated: 29/Oct/23  Resolved: 21/Dec/22

Status: Closed
Project: Core Server
Component/s: None
Affects Version/s: None
Fix Version/s: 6.3.0-rc0, 6.0.11

Type: Bug Priority: Major - P3
Reporter: Yujin Kang Park Assignee: Yujin Kang Park
Resolution: Fixed Votes: 0
Labels: None
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified

Issue Links:
Backports
Depends
Problem/Incident
Backwards Compatibility: Fully Compatible
Operating System: ALL
Backport Requested:
v6.0
Sprint: Execution Team 2022-12-12, Execution Team 2022-12-26
Participants:
Linked BF Score: 135

 Description   

    assert.commandWorkedOrFailedWithCode = function commandWorkedOrFailedWithCode(res, errorCodeSet, msg) {
        if (!res.ok) {
            return assert.commandFailedWithCode(res, errorCodeSet, msg);
        } else {
            return assert.commandWorked(res, msg);
        }
    }; 

But not all operations, such as insert, have the 'ok' field in the response. For those operations the commandFailedWithCode branch is always taken. 

Possible fix:

    assert.commandWorkedOrFailedWithCode = function commandWorkedOrFailedWithCode(res, errorCodeSet, msg) {
        try {            return assert.commandWorked(res, msg);        } 
        catch (e) {            return assert.commandFailedWithCode(res, errorCodeSet, msg);        }    
}; 



 Comments   
Comment by Githook User [ 04/Sep/23 ]

Author:

{'name': 'Yu Jin Kang Park', 'email': 'yujin.kang@mongodb.com', 'username': 'ykangpark'}

Message: SERVER-71512 Fix assert.commandWorkedOrFailedWithCode
Branch: v6.0
https://github.com/mongodb/mongo/commit/d2130103a5793891643c4844ce5017d106ee3d61

Comment by Githook User [ 21/Dec/22 ]

Author:

{'name': 'Yu Jin Kang Park', 'email': 'yujin.kang@mongodb.com', 'username': 'ykangpark'}

Message: SERVER-71512 Fix assert.commandWorkedOrFailedWithCode
Branch: master
https://github.com/mongodb/mongo/commit/1ad63b903d3a2be8ddc29072f2996863bde32ade

Comment by Githook User [ 20/Dec/22 ]

Author:

{'name': 'dziana.hurynovich', 'email': 'dziana.hurynovich@mongodb.com'}

Message: Revert "SERVER-71512 Fix assert.commandWorkedOrFailedWithCode"

This reverts commit 72b203af76e771c0705bfa657c9df76fa48c4cd5.
Branch: master
https://github.com/mongodb/mongo/commit/f84b645556a27b078b61260d55c53b664aff8b7f

Comment by Githook User [ 19/Dec/22 ]

Author:

{'name': 'Yu Jin Kang Park', 'email': 'yujin.kang@mongodb.com', 'username': 'ykangpark'}

Message: SERVER-71512 Fix assert.commandWorkedOrFailedWithCode
Branch: master
https://github.com/mongodb/mongo/commit/72b203af76e771c0705bfa657c9df76fa48c4cd5

Comment by Yujin Kang Park [ 23/Nov/22 ]

charlie.swanson@mongodb.com thanks for the input! My thought was that it was wrongly assumed that all command types had the ok field, so I opened this ticket with exactly that intention, leveraging the already existing more nuanced logic within commandWorked.

Comment by Charlie Swanson [ 22/Nov/22 ]

The assert.commandWorked() function has some more complicated/nuanced logic to see if something failed than just checking res.ok. The suggestion in the ticket seems fine to me, but just for the record and consideration, here is that other logic:
https://github.com/10gen/mongo/blob/31d0b62c253ca827c5d1e209e21670199b0a8a30/src/mongo/shell/assert.js#L746-L773

Generated at Thu Feb 08 06:19:13 UTC 2024 using Jira 9.7.1#970001-sha1:2222b88b221c4928ef0de3161136cc90c8356a66.