Skip to content

Commit

Permalink
fix(NODE-6276): preserve top level error code MongoWriteConcernError (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
aditi-khare-mongoDB authored Jul 26, 2024
1 parent 9de03fd commit e902584
Show file tree
Hide file tree
Showing 5 changed files with 54 additions and 14 deletions.
4 changes: 2 additions & 2 deletions src/bulk/common.ts
Original file line number Diff line number Diff line change
Expand Up @@ -616,8 +616,8 @@ function handleMongoWriteConcernError(
callback(
new MongoBulkWriteError(
{
message: err.result?.writeConcernError.errmsg,
code: err.result?.writeConcernError.result
message: err.result.writeConcernError.errmsg,
code: err.result.writeConcernError.code
},
new BulkWriteResult(bulkResult, isOrdered)
)
Expand Down
31 changes: 20 additions & 11 deletions src/error.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1158,6 +1158,23 @@ export class MongoServerSelectionError extends MongoSystemError {
}
}

/**
* The type of the result property of MongoWriteConcernError
* @public
*/
export interface WriteConcernErrorResult {
writeConcernError: {
code: number;
errmsg: string;
codeName?: string;
errInfo?: Document;
};
ok: number;
code?: number;
errorLabels?: string[];
[x: string | number]: unknown;
}

/**
* An error thrown when the server reports a writeConcernError
* @public
Expand All @@ -1178,16 +1195,8 @@ export class MongoWriteConcernError extends MongoServerError {
*
* @public
**/
constructor(result: {
writeConcernError: {
code: number;
errmsg: string;
codeName?: string;
errInfo?: Document;
};
errorLabels?: string[];
}) {
super({ ...result, ...result.writeConcernError });
constructor(result: WriteConcernErrorResult) {
super({ ...result.writeConcernError, ...result });
this.errInfo = result.writeConcernError.errInfo;
this.result = result;
}
Expand Down Expand Up @@ -1237,7 +1246,7 @@ export function needsRetryableWriteLabel(error: Error, maxWireVersion: number):
}

if (error instanceof MongoWriteConcernError) {
return RETRYABLE_WRITE_ERROR_CODES.has(error.result?.code ?? error.code ?? 0);
return RETRYABLE_WRITE_ERROR_CODES.has(error.result.writeConcernError.code ?? error?.code ?? 0);
}

if (error instanceof MongoError && typeof error.code === 'number') {
Expand Down
3 changes: 2 additions & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,8 @@ export {
MongoTopologyClosedError,
MongoTransactionError,
MongoUnexpectedServerResponseError,
MongoWriteConcernError
MongoWriteConcernError,
WriteConcernErrorResult
} from './error';
export {
AbstractCursor,
Expand Down
29 changes: 29 additions & 0 deletions test/unit/error.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -740,4 +740,33 @@ describe('MongoErrors', () => {
});
});
});

describe('MongoWriteConcernError constructor', function () {
context('when no top-level code is provided', function () {
it('error.code is set to writeConcernError.code', function () {
const res = {
writeConcernError: {
code: 81, // nested code
errmsg: 'fake msg'
},
ok: 1
};
expect(new MongoWriteConcernError(res).code).to.equal(81);
});
});
context('when top-level code is provided and writeConcernError.code exists', function () {
it('error.code equals the top-level code', function () {
const topLevelCode = 10;
const res = {
writeConcernError: {
code: 81, // nested code
errmsg: 'fake msg'
},
ok: 1,
code: topLevelCode
};
expect(new MongoWriteConcernError(res).code).to.equal(topLevelCode);
});
});
});
});
1 change: 1 addition & 0 deletions test/unit/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,7 @@ const EXPECTED_EXPORTS = [
'MongoTransactionError',
'MongoUnexpectedServerResponseError',
'MongoWriteConcernError',
'WriteConcernErrorResult',
'ObjectId',
'OrderedBulkOperation',
'ProfilingLevel',
Expand Down

0 comments on commit e902584

Please sign in to comment.