Skip to content

Commit ee69019

Browse files
committed
Refactor argument error message handling
1 parent e919b47 commit ee69019

20 files changed

+172
-140
lines changed

lib/aqQueue.js

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -26,9 +26,7 @@ const nodbUtil = require('./util.js');
2626
// Returns a single message from the queue, if one is available.
2727
//-----------------------------------------------------------------------------
2828
function deqOne(cb) {
29-
nodbUtil.assert(arguments.length === 1, 'NJS-009');
30-
nodbUtil.assert(typeof cb === 'function', 'NJS-005', 1);
31-
29+
nodbUtil.checkAsyncArgs(arguments, 1, 1);
3230
this._deqOne(cb);
3331
}
3432

@@ -39,9 +37,8 @@ function deqOne(cb) {
3937
// if any are available.
4038
//----------------------------------------------------------------------------
4139
function deqMany(maxMessages, cb) {
42-
nodbUtil.assert(arguments.length === 2, 'NJS-009');
40+
nodbUtil.checkAsyncArgs(arguments, 2, 2);
4341
nodbUtil.assert(typeof maxMessages === 'number', 'NJS-005', 1);
44-
nodbUtil.assert(typeof cb === 'function', 'NJS-005', 2);
4542

4643
this._deqMany(maxMessages, cb);
4744
}
@@ -52,10 +49,9 @@ function deqMany(maxMessages, cb) {
5249
// Enqueues a single message into the queue.
5350
//-----------------------------------------------------------------------------
5451
function enqOne(message, cb) {
55-
nodbUtil.assert(arguments.length === 2, 'NJS-009');
52+
nodbUtil.checkAsyncArgs(arguments, 2, 2);
5653
nodbUtil.assert(typeof message === 'object' || typeof message === 'string',
5754
'NJS-005', 1);
58-
nodbUtil.assert(typeof cb === 'function', 'NJS-005', 2);
5955

6056
this._enqOne(message, cb);
6157
}
@@ -67,9 +63,8 @@ function enqOne(message, cb) {
6763
// if any are available.
6864
//----------------------------------------------------------------------------
6965
function enqMany(messages, cb) {
70-
nodbUtil.assert(arguments.length === 2, 'NJS-009');
66+
nodbUtil.checkAsyncArgs(arguments, 2, 2);
7167
nodbUtil.assert(Array.isArray(messages), 'NJS-005', 1);
72-
nodbUtil.assert(typeof cb === 'function', 'NJS-005', 2);
7368

7469
this._enqMany(messages, cb);
7570
}

lib/connection.js

Lines changed: 17 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -73,24 +73,21 @@ function execute(sql, a2, a3, a4) {
7373
let executeCb;
7474
let custExecuteCb;
7575

76-
nodbUtil.assert(arguments.length > 1 && arguments.length < 5, 'NJS-009');
76+
nodbUtil.checkAsyncArgs(arguments, 2, 4);
7777
nodbUtil.assert(typeof sql === 'string', 'NJS-005', 1);
7878

7979
switch (arguments.length) {
8080
case 2:
81-
nodbUtil.assert(typeof a2 === 'function', 'NJS-005', 2);
8281
executeCb = a2;
8382
break;
8483
case 3:
8584
nodbUtil.assert(nodbUtil.isObjectOrArray(a2), 'NJS-005', 2);
86-
nodbUtil.assert(typeof a3 === 'function', 'NJS-005', 3);
8785
binds = a2;
8886
executeCb = a3;
8987
break;
9088
case 4:
9189
nodbUtil.assert(nodbUtil.isObjectOrArray(a2), 'NJS-005', 2);
9290
nodbUtil.assert(nodbUtil.isObject(a3), 'NJS-005', 3);
93-
nodbUtil.assert(typeof a4 === 'function', 'NJS-005', 4);
9491
binds = a2;
9592
executeOpts = a3;
9693
executeCb = a4;
@@ -177,7 +174,7 @@ function executeMany(sql, bindsOrNumIters, a3, a4) {
177174
let executeCb;
178175
let okBinds;
179176

180-
nodbUtil.assert(arguments.length > 2 && arguments.length < 5, 'NJS-009');
177+
nodbUtil.checkAsyncArgs(arguments, 3, 4);
181178
nodbUtil.assert(typeof sql === 'string', 'NJS-005', 1);
182179
if (typeof bindsOrNumIters === 'number') {
183180
nodbUtil.assert(Number.isInteger(bindsOrNumIters), 'NJS-005', 2);
@@ -189,12 +186,10 @@ function executeMany(sql, bindsOrNumIters, a3, a4) {
189186

190187
switch (arguments.length) {
191188
case 3:
192-
nodbUtil.assert(typeof a3 === 'function', 'NJS-005', 3);
193189
executeCb = a3;
194190
break;
195191
case 4:
196192
nodbUtil.assert(nodbUtil.isObject(a3), 'NJS-005', 3);
197-
nodbUtil.assert(typeof a4 === 'function', 'NJS-005', 4);
198193
options = a3;
199194
executeCb = a4;
200195
break;
@@ -211,9 +206,8 @@ function executeMany(sql, bindsOrNumIters, a3, a4) {
211206
// first, but if not found, the database is queried and the result is cached
212207
// using the fully qualified name
213208
function getDbObjectClass(name, cb) {
214-
nodbUtil.assert(arguments.length === 2, 'NJS-009');
209+
nodbUtil.checkAsyncArgs(arguments, 2, 2);
215210
nodbUtil.assert(typeof name === 'string', 'NJS-005', 1);
216-
nodbUtil.assert(typeof cb === 'function', 'NJS-005', 2);
217211

218212
// check the cache; if the class is found there, nothing further to do
219213
let cls = this._dbObjectClasses[name];
@@ -231,8 +225,7 @@ function getDbObjectClass(name, cb) {
231225
function getStatementInfo(sql, getStatementInfoCb) {
232226
const self = this;
233227

234-
nodbUtil.assert(arguments.length === 2, 'NJS-009');
235-
nodbUtil.assert(typeof getStatementInfoCb === 'function', 'NJS-005', 1);
228+
nodbUtil.checkAsyncArgs(arguments, 2, 2);
236229

237230
self._getStatementInfo.apply(self, arguments);
238231
}
@@ -241,18 +234,15 @@ function getStatementInfo(sql, getStatementInfoCb) {
241234
function commit(commitCb) {
242235
const self = this;
243236

244-
nodbUtil.assert(arguments.length === 1, 'NJS-009');
245-
nodbUtil.assert(typeof commitCb === 'function', 'NJS-005', 1);
246-
237+
nodbUtil.checkAsyncArgs(arguments, 1, 1);
247238
self._commit.apply(self, arguments);
248239
}
249240

250241
// This createLob function is just a place holder to allow for easier extension later.
251242
function createLob(type, createLobCb) {
252243
const self = this;
253244

254-
nodbUtil.assert(arguments.length === 2, 'NJS-009');
255-
nodbUtil.assert(typeof createLobCb === 'function', 'NJS-005', 2);
245+
nodbUtil.checkAsyncArgs(arguments, 2, 2);
256246

257247
self._createLob.apply(self, arguments);
258248
}
@@ -261,8 +251,7 @@ function createLob(type, createLobCb) {
261251
function rollback(rollbackCb) {
262252
const self = this;
263253

264-
nodbUtil.assert(arguments.length === 1, 'NJS-009');
265-
nodbUtil.assert(typeof rollbackCb === 'function', 'NJS-005', 1);
254+
nodbUtil.checkAsyncArgs(arguments, 1, 1);
266255

267256
self._rollback.apply(self, arguments);
268257
}
@@ -274,16 +263,14 @@ function close(a1, a2) {
274263
let options = {};
275264
let closeCb;
276265

277-
nodbUtil.assert(arguments.length >= 1 && arguments.length <= 2, 'NJS-009');
266+
nodbUtil.checkAsyncArgs(arguments, 1, 2);
278267

279268
switch (arguments.length) {
280269
case 1:
281-
nodbUtil.assert(typeof a1 === 'function', 'NJS-005', 1);
282270
closeCb = a1;
283271
break;
284272
case 2:
285273
nodbUtil.assert(nodbUtil.isObject(a1), 'NJS-005', 1);
286-
nodbUtil.assert(typeof a2 === 'function', 'NJS-005', 2);
287274
options = a1;
288275
closeCb = a2;
289276
break;
@@ -307,8 +294,7 @@ function close(a1, a2) {
307294
module.break = function(breakCb) {
308295
const self = this;
309296

310-
nodbUtil.assert(arguments.length === 1, 'NJS-009');
311-
nodbUtil.assert(typeof breakCb === 'function', 'NJS-005', 1);
297+
nodbUtil.checkAsyncArgs(arguments, 1, 1);
312298

313299
self._break.apply(self, arguments);
314300
};
@@ -317,7 +303,7 @@ module.break = function(breakCb) {
317303
function changePassword(user, password, newPassword, changePasswordCb) {
318304
const self = this;
319305

320-
nodbUtil.assert(arguments.length === 4, 'NJS-009');
306+
nodbUtil.checkAsyncArgs(arguments, 4, 4);
321307
nodbUtil.assert(typeof user === 'string', 'NJS-005', 1);
322308
nodbUtil.assert(typeof password === 'string', 'NJS-005', 2);
323309
nodbUtil.assert(typeof newPassword === 'string', 'NJS-005', 3);
@@ -330,16 +316,15 @@ function changePassword(user, password, newPassword, changePasswordCb) {
330316
function getQueue(name, a2, a3) {
331317
let options = {};
332318
let queueCb;
333-
nodbUtil.assert(arguments.length >= 2 && arguments.length <= 3, 'NJS-009');
319+
320+
nodbUtil.checkAsyncArgs(arguments, 2, 3);
334321
nodbUtil.assert(typeof name === 'string', 'NJS-005', 1);
335322
switch (arguments.length) {
336323
case 2:
337-
nodbUtil.assert(typeof a2 === 'function', 'NJS-005', 2);
338324
queueCb = a2;
339325
break;
340326
case 3:
341327
nodbUtil.assert(nodbUtil.isObject(a2), 'NJS-005', 2);
342-
nodbUtil.assert(typeof a3 === 'function', 'NJS-005', 3);
343328
options = a2;
344329
queueCb = a3;
345330
break;
@@ -351,9 +336,7 @@ function getQueue(name, a2, a3) {
351336
function ping(pingCb) {
352337
const self = this;
353338

354-
nodbUtil.assert(arguments.length === 1, 'NJS-009');
355-
nodbUtil.assert(typeof pingCb === 'function', 'NJS-005', 1);
356-
339+
nodbUtil.checkAsyncArgs(arguments, 1, 1);
357340
self._ping.apply(self, arguments);
358341
}
359342

@@ -362,20 +345,18 @@ function ping(pingCb) {
362345
function subscribe(name, options, subscribeCb) {
363346
const self = this;
364347

365-
nodbUtil.assert(arguments.length == 3, 'NJS-009');
348+
nodbUtil.checkAsyncArgs(arguments, 3, 3);
366349
nodbUtil.assert(typeof name === 'string', 'NJS-005', 1);
367350
nodbUtil.assert(nodbUtil.isObject(options), 'NJS-005', 2);
368-
nodbUtil.assert(typeof subscribeCb === 'function', 'NJS-005', 3);
369351
self._subscribe.call(self, name, options, subscribeCb);
370352
}
371353

372354
// destroy a subscription which was earlier created using subscribe()
373355
function unsubscribe(name, cb) {
374356
const self = this;
375357

376-
nodbUtil.assert(arguments.length == 2, 'NJS-009');
358+
nodbUtil.checkAsyncArgs(arguments, 2, 2);
377359
nodbUtil.assert(typeof name === 'string', 'NJS-005', 1);
378-
nodbUtil.assert(typeof cb === 'function', 'NJS-005', 2);
379360

380361
self._unsubscribe.call(self, name, cb);
381362
}
@@ -447,7 +428,7 @@ class Connection extends EventEmitter {
447428
// To obtain a SodaDatabase object (high-level SODA object associated with
448429
// current connection)
449430
getSodaDatabase() {
450-
nodbUtil.assert(arguments.length === 0, 'NJS-009');
431+
nodbUtil.checkArgCount(arguments, 0, 0);
451432
return this._getSodaDatabase();
452433
}
453434

@@ -457,7 +438,7 @@ class Connection extends EventEmitter {
457438
const self = this;
458439
let stream;
459440

460-
nodbUtil.assert(arguments.length > 0 && arguments.length < 4, 'NJS-009');
441+
nodbUtil.checkArgCount(arguments, 1, 3);
461442
nodbUtil.assert(typeof sql === 'string', 'NJS-005', 1);
462443

463444
if (binding) {

lib/lob.js

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,7 @@ const nodbUtil = require('./util.js');
2424
const util = require('util');
2525

2626
function close(closeCb) {
27-
nodbUtil.assert(arguments.length === 1, 'NJS-009');
28-
nodbUtil.assert(typeof closeCb === 'function', 'NJS-005', 1);
27+
nodbUtil.checkAsyncArgs(arguments, 1, 1);
2928

3029
// Return if LOB already closed to support multiple close() calls should be
3130
// no-op
@@ -47,8 +46,7 @@ function close(closeCb) {
4746

4847

4948
function getData(getDataCb) {
50-
nodbUtil.assert(arguments.length === 1, 'NJS-009');
51-
nodbUtil.assert(typeof getDataCb === 'function', 'NJS-005', 1);
49+
nodbUtil.checkAsyncArgs(arguments, 1, 1);
5250
this._getData(getDataCb);
5351
}
5452

lib/oracledb.js

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@ class OracleDb {
9898
getPool(poolAlias) {
9999
let pool;
100100

101-
nodbUtil.assert(arguments.length < 2, 'NJS-009');
101+
nodbUtil.checkArgCount(arguments, 0, 1);
102102

103103
if (poolAlias) {
104104
nodbUtil.assert(typeof poolAlias === 'string' || typeof poolAlias === 'number', 'NJS-005', 1);
@@ -133,9 +133,8 @@ function createPool(poolAttrs, createPoolCb) {
133133

134134
// Initial argument count and type checks are done first and throw in the same
135135
// call stack.
136-
nodbUtil.assert(arguments.length === 2, 'NJS-009');
136+
nodbUtil.checkAsyncArgs(arguments, 2, 2);
137137
nodbUtil.assert(nodbUtil.isObject(poolAttrs), 'NJS-005', 1);
138-
nodbUtil.assert(typeof createPoolCb === 'function', 'NJS-005', 2);
139138

140139
// Additional validations should pass errors via the callback. Need to ensure
141140
// that errors are raised prior to actually creating the pool via _createPool.
@@ -224,21 +223,17 @@ function getConnection(a1, a2) {
224223
let connAttrs = {};
225224
let getConnectionCb;
226225

227-
nodbUtil.assert(arguments.length < 3, 'NJS-009');
226+
nodbUtil.checkAsyncArgs(arguments, 1, 2);
228227

229228
// Verify the number and types of arguments, then initialize the local poolAlias,
230229
// connAttrs, and getConnectionCb variables based on the arguments.
231230
switch (arguments.length) {
232231
case 1:
233-
nodbUtil.assert(typeof a1 === 'function', 'NJS-005', 1);
234-
235232
poolAlias = defaultPoolAlias;
236233
getConnectionCb = a1;
237-
238234
break;
239235
case 2:
240236
nodbUtil.assert(typeof a1 === 'string' || nodbUtil.isObject(a1), 'NJS-005', 1);
241-
nodbUtil.assert(typeof a2 === 'function', 'NJS-005', 2);
242237

243238
if (typeof a1 === 'string') {
244239
poolAlias = a1;

lib/pool.js

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -156,7 +156,7 @@ function getConnection(a1, a2) {
156156
let getConnectionCb;
157157
let options = {};
158158

159-
nodbUtil.assert(arguments.length >= 1 && arguments.length <= 2, 'NJS-009');
159+
nodbUtil.checkAsyncArgs(arguments, 1, 2);
160160
switch (arguments.length) {
161161
case 1:
162162
getConnectionCb = a1;
@@ -167,7 +167,6 @@ function getConnection(a1, a2) {
167167
getConnectionCb = a2;
168168
break;
169169
}
170-
nodbUtil.assert(typeof getConnectionCb === 'function', 'NJS-005', arguments.length);
171170

172171
if (self.status === self._oracledb.POOL_STATUS_DRAINING) { // closing soon
173172
getConnectionCb(new Error(nodbUtil.getErrorMessage('NJS-064')));
@@ -238,7 +237,7 @@ function close(a1, a2) {
238237
let timeoutCb;
239238
let closeCb;
240239

241-
nodbUtil.assert(arguments.length === 1 || arguments.length === 2, 'NJS-009');
240+
nodbUtil.checkArgCount(arguments, 1, 2);
242241

243242
switch (arguments.length) {
244243
case 1:

lib/resultset.js

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,7 @@ const nodbUtil = require('./util.js');
2626
function close(closeCb) {
2727
const self = this;
2828

29-
nodbUtil.assert(arguments.length === 1, 'NJS-009');
30-
nodbUtil.assert(typeof closeCb === 'function', 'NJS-005', 1);
29+
nodbUtil.checkAsyncArgs(arguments, 1, 1);
3130

3231
if (self._convertedToStream) {
3332
closeCb(new Error(nodbUtil.getErrorMessage('NJS-042')));
@@ -49,8 +48,7 @@ function close(closeCb) {
4948
function getRow(getRowCb) {
5049
const self = this;
5150

52-
nodbUtil.assert(arguments.length === 1, 'NJS-009');
53-
nodbUtil.assert(typeof getRowCb === 'function', 'NJS-005', 1);
51+
nodbUtil.checkAsyncArgs(arguments, 1, 1);
5452

5553
if (self._convertedToStream && !self._allowGetRowCall) {
5654
getRowCb(new Error(nodbUtil.getErrorMessage('NJS-042')));
@@ -91,10 +89,9 @@ function getRows(numRows, getRowsCb) {
9189
const self = this;
9290
let rowsNeeded;
9391

94-
nodbUtil.assert(arguments.length === 2, 'NJS-009');
92+
nodbUtil.checkAsyncArgs(arguments, 2, 2);
9593
nodbUtil.assert(Number.isInteger(numRows), 'NJS-005', 2);
9694
nodbUtil.assert(numRows > 0, 'NJS-005', 2);
97-
nodbUtil.assert(typeof getRowsCb === 'function', 'NJS-005', 2);
9895

9996
if (self._convertedToStream) {
10097
getRowsCb(new Error(nodbUtil.getErrorMessage('NJS-042')));
@@ -160,7 +157,7 @@ class ResultSet {
160157
toQueryStream() {
161158
const self = this;
162159

163-
nodbUtil.assert(arguments.length === 0, 'NJS-009');
160+
nodbUtil.checkArgCount(arguments, 0, 0);
164161

165162
if (self._processingStarted) {
166163
throw new Error(nodbUtil.getErrorMessage('NJS-041'));

0 commit comments

Comments
 (0)