Skip to content

Commit d1e0224

Browse files
author
Kwabena Ampofo
committed
Throw specific error in the case of a sparse array, create new test to check the expected error message has not been changed, fix grammar issues
1 parent f7cce97 commit d1e0224

File tree

2 files changed

+26
-4
lines changed

2 files changed

+26
-4
lines changed

src/operations/insert.ts

+8-1
Original file line numberDiff line numberDiff line change
@@ -136,7 +136,14 @@ export class InsertManyOperation extends AbstractOperation<InsertManyResult> {
136136
);
137137

138138
bulkWriteOperation.execute(server, session, (err, res) => {
139-
if (err || res == null) return callback(err);
139+
if (err || res == null) {
140+
if (err && err.message === 'Operation must be an object with an operation key') {
141+
err = new MongoInvalidArgumentError(
142+
'Argument "docs" is a sparse array containing element(s) that are null or undefined'
143+
);
144+
}
145+
return callback(err);
146+
}
140147
callback(undefined, {
141148
acknowledged: writeConcern?.w !== 0 ?? true,
142149
insertedCount: res.insertedCount,

test/integration/crud/bulk.test.js

+18-3
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,8 @@ describe('Bulk', function () {
2828
return setupDatabase(this.configuration);
2929
});
3030
describe('BulkOperationBase', () => {
31-
describe('#raw', function () {
31+
// eslint-disable-next-line no-restricted-properties
32+
describe.only('#raw', function () {
3233
let client;
3334
beforeEach(async function () {
3435
client = this.configuration.newClient();
@@ -44,6 +45,18 @@ describe('Bulk', function () {
4445
expect(() => bulkOp.raw(true)).to.throw(MongoInvalidArgumentError);
4546
expect(() => bulkOp.raw(3)).to.throw(MongoInvalidArgumentError);
4647
});
48+
49+
it('should throw an error with the specifc message: "Operation must be an object with an operation key"', async function () {
50+
const bulkOp = client.db('test').collection('test').initializeUnorderedBulkOp();
51+
try {
52+
bulkOp.raw(undefined);
53+
expect.fail(
54+
'Expected passing argument of "undefined" to .raw to throw error, failed to throw error'
55+
);
56+
} catch (error) {
57+
expect(error.message).to.equal('Operation must be an object with an operation key');
58+
}
59+
});
4760
});
4861

4962
context('when called with a valid operation', function () {
@@ -58,6 +71,8 @@ describe('Bulk', function () {
5871
});
5972
});
6073

74+
//write test to check if error thrown at raw op hasnt changed
75+
6176
describe('Db.collection', function () {
6277
describe('#insertMany', function () {
6378
let client;
@@ -68,8 +83,8 @@ describe('Bulk', function () {
6883
afterEach(async function () {
6984
await client.close();
7085
});
71-
context('when passed an invalid or sparse list', function () {
72-
it('insertMany should throw a MongoInvalidArgument error when called with a valid operation', async function () {
86+
context('when passed an invalid docs argument', function () {
87+
it('insertMany should throw a MongoInvalidArgument error when called with a invalid operation', async function () {
7388
try {
7489
const docs = [];
7590
docs[1] = { color: 'red' };

0 commit comments

Comments
 (0)