Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
fix(NODE-6276): preserve top level error code MongoWriteConcernError
  • Loading branch information
aditi-khare-mongoDB committed Jul 25, 2024
commit 623b5fce5da07b7ccfc28bdd3167ed3f9c2518a0
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
37 changes: 24 additions & 13 deletions src/error.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1158,14 +1158,30 @@ 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: 0 | 1;
code?: number;
[x: string | number]: unknown;
}

/**
* An error thrown when the server reports a writeConcernError
* @public
* @category Error
*/
export class MongoWriteConcernError extends MongoServerError {
/** The result document */
result: Document;
result: WriteConcernErrorResult;

/**
* **Do not use this constructor!**
Expand All @@ -1178,18 +1194,11 @@ export class MongoWriteConcernError extends MongoServerError {
*
* @public
**/
constructor(result: {
writeConcernError: {
code: number;
errmsg: string;
codeName?: string;
errInfo?: Document;
};
errorLabels?: string[];
}) {
super({ ...result, ...result.writeConcernError });
this.errInfo = result.writeConcernError.errInfo;
constructor(result: WriteConcernErrorResult) {
super(result);
this.errInfo = result.writeConcernError?.errInfo;
this.result = result;
this.code = result.code ? result.code : undefined;
}

override get name(): string {
Expand Down Expand Up @@ -1237,7 +1246,9 @@ 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
32 changes: 31 additions & 1 deletion test/unit/error.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,8 @@ import {
setDifference,
type TopologyDescription,
type TopologyOptions,
WaitQueueTimeoutError as MongoWaitQueueTimeoutError
WaitQueueTimeoutError as MongoWaitQueueTimeoutError,
WriteConcernErrorResult
} from '../mongodb';
import { ReplSetFixture } from '../tools/common';
import { cleanup } from '../tools/mongodb-mock/index';
Expand Down Expand Up @@ -740,4 +741,33 @@ describe('MongoErrors', () => {
});
});
});

describe('MongoWriteConcernError constructor', function () {
context('when no top-level code is provided and writeConcernError.code exists', function () {
it('error.code remains undefined', function () {
const res: WriteConcernErrorResult = {
writeConcernError: {
code: 81, // nested code
errmsg: 'fake msg'
},
ok: 1
};
expect(new MongoWriteConcernError(res).code).to.equal(undefined);
});
});
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: WriteConcernErrorResult = {
writeConcernError: {
code: 81, // nested code
errmsg: 'fake msg'
},
ok: 1,
code: topLevelCode
};
expect(new MongoWriteConcernError(res).code).to.equal(topLevelCode);
});
});
});
});
2 changes: 1 addition & 1 deletion test/unit/mongo_client.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -737,7 +737,7 @@ describe('MongoClient', function () {
expect(error).to.have.property('code', 'EBADNAME');
});

it('srvServiceName should not error if it is greater than 15 characters as long as the DNS query limit is not surpassed', async () => {
it.only('srvServiceName should not error if it is greater than 15 characters as long as the DNS query limit is not surpassed', async () => {
const options = parseOptions('mongodb+srv://localhost.a.com', {
srvServiceName: 'a'.repeat(16)
});
Expand Down