Skip to content

Commit 443743d

Browse files
committed
Tidy up empty array errors in con.executeMany() and coll.insertMany()
1 parent 790f874 commit 443743d

File tree

4 files changed

+27
-16
lines changed

4 files changed

+27
-16
lines changed

CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,9 @@
106106
- The NJS-014 error when setting a read-only property was replaced
107107
with a standard JavaScript message.
108108

109+
- When passing 0 or a negative value for the number of iterations to
110+
`connection.executeMany()`, errors now occur through the error callback.
111+
109112
- Some error numbers may have changed due to code refactoring.
110113
Some message text was updated.
111114

lib/connection.js

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -175,14 +175,16 @@ function executeMany(sql, bindsOrNumIters, a3, a4) {
175175
const self = this;
176176
let options = {};
177177
let executeCb;
178+
let okBinds;
178179

179180
nodbUtil.assert(arguments.length > 2 && arguments.length < 5, 'NJS-009');
180181
nodbUtil.assert(typeof sql === 'string', 'NJS-005', 1);
181182
if (typeof bindsOrNumIters === 'number') {
182-
nodbUtil.assert(Number.isInteger(bindsOrNumIters) && bindsOrNumIters > 0,
183-
'NJS-005', 2);
183+
nodbUtil.assert(Number.isInteger(bindsOrNumIters), 'NJS-005', 2);
184+
okBinds = (bindsOrNumIters > 0);
184185
} else {
185186
nodbUtil.assert(Array.isArray(bindsOrNumIters), 'NJS-005', 2);
187+
okBinds = (bindsOrNumIters.length > 0);
186188
}
187189

188190
switch (arguments.length) {
@@ -197,6 +199,10 @@ function executeMany(sql, bindsOrNumIters, a3, a4) {
197199
executeCb = a4;
198200
break;
199201
}
202+
if (!okBinds) {
203+
executeCb(new Error(nodbUtil.getErrorMessage('NJS-005', 2)));
204+
return;
205+
}
200206

201207
self._executeMany.call(self, sql, bindsOrNumIters, options, executeCb);
202208
}

lib/sodaCollection.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,10 @@ function insertMany(docs, cb) {
3535
nodbUtil.assert(arguments.length === 2, 'NJS-009');
3636
nodbUtil.assert(Array.isArray(docs), 'NJS-005', 1);
3737
nodbUtil.assert(typeof cb === 'function', 'NJS-005', 2);
38+
if (docs.length == 0) {
39+
cb(new Error(nodbUtil.getErrorMessage('NJS-005', 1)));
40+
return;
41+
}
3842

3943
let actualDocs = Array(docs.length);
4044
for (let i = 0; i < docs.length; i++) {
@@ -55,6 +59,10 @@ function insertManyAndGet(docs, cb) {
5559
nodbUtil.assert(arguments.length === 2, 'NJS-009');
5660
nodbUtil.assert(Array.isArray(docs), 'NJS-005', 1);
5761
nodbUtil.assert(typeof cb === 'function', 'NJS-005', 2);
62+
if (docs.length == 0) {
63+
cb(new Error(nodbUtil.getErrorMessage('NJS-005', 1)));
64+
return;
65+
}
5866

5967
let actualDocs = Array(docs.length);
6068
for (let i = 0; i < docs.length; i++) {

test/executeMany1.js

Lines changed: 8 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -659,7 +659,7 @@ describe('163. executeMany1.js', function() {
659659
it('163.15 Negative - set numIterations to be negative value', function(done) {
660660
async.series([
661661
function(cb) {
662-
var sql = `
662+
const sql = `
663663
declare
664664
t_Id number;
665665
begin
@@ -670,19 +670,13 @@ describe('163. executeMany1.js', function() {
670670
values (t_Id, 'Test String ' || t_Id);
671671
end;`;
672672

673-
var numIterations = -8;
674-
675-
should.throws(
676-
function() {
677-
conn.executeMany(
678-
sql,
679-
numIterations,
680-
function() { }
681-
);
682-
},
683-
/NJS-005: invalid value for parameter 2/
684-
);
685-
cb();
673+
const numIterations = -8;
674+
conn.executeMany(sql, numIterations, function(err, result) {
675+
should.exist(err);
676+
err.message.should.startWith('NJS-005:');
677+
should.not.exist(result);
678+
cb();
679+
});
686680
},
687681
function(cb) {
688682
dotruncate(cb);

0 commit comments

Comments
 (0)