Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(NODE-6276): preserve top level error code MongoWriteConcernError #4183

Merged
merged 5 commits into from
Jul 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
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
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