Refactor argument error message handling

This commit is contained in:
Christopher Jones 2019-11-05 11:17:39 +11:00
parent e919b47051
commit ee69019505
20 changed files with 172 additions and 140 deletions

View File

@ -26,9 +26,7 @@ const nodbUtil = require('./util.js');
// Returns a single message from the queue, if one is available.
//-----------------------------------------------------------------------------
function deqOne(cb) {
nodbUtil.assert(arguments.length === 1, 'NJS-009');
nodbUtil.assert(typeof cb === 'function', 'NJS-005', 1);
nodbUtil.checkAsyncArgs(arguments, 1, 1);
this._deqOne(cb);
}
@ -39,9 +37,8 @@ function deqOne(cb) {
// if any are available.
//----------------------------------------------------------------------------
function deqMany(maxMessages, cb) {
nodbUtil.assert(arguments.length === 2, 'NJS-009');
nodbUtil.checkAsyncArgs(arguments, 2, 2);
nodbUtil.assert(typeof maxMessages === 'number', 'NJS-005', 1);
nodbUtil.assert(typeof cb === 'function', 'NJS-005', 2);
this._deqMany(maxMessages, cb);
}
@ -52,10 +49,9 @@ function deqMany(maxMessages, cb) {
// Enqueues a single message into the queue.
//-----------------------------------------------------------------------------
function enqOne(message, cb) {
nodbUtil.assert(arguments.length === 2, 'NJS-009');
nodbUtil.checkAsyncArgs(arguments, 2, 2);
nodbUtil.assert(typeof message === 'object' || typeof message === 'string',
'NJS-005', 1);
nodbUtil.assert(typeof cb === 'function', 'NJS-005', 2);
this._enqOne(message, cb);
}
@ -67,9 +63,8 @@ function enqOne(message, cb) {
// if any are available.
//----------------------------------------------------------------------------
function enqMany(messages, cb) {
nodbUtil.assert(arguments.length === 2, 'NJS-009');
nodbUtil.checkAsyncArgs(arguments, 2, 2);
nodbUtil.assert(Array.isArray(messages), 'NJS-005', 1);
nodbUtil.assert(typeof cb === 'function', 'NJS-005', 2);
this._enqMany(messages, cb);
}

View File

@ -73,24 +73,21 @@ function execute(sql, a2, a3, a4) {
let executeCb;
let custExecuteCb;
nodbUtil.assert(arguments.length > 1 && arguments.length < 5, 'NJS-009');
nodbUtil.checkAsyncArgs(arguments, 2, 4);
nodbUtil.assert(typeof sql === 'string', 'NJS-005', 1);
switch (arguments.length) {
case 2:
nodbUtil.assert(typeof a2 === 'function', 'NJS-005', 2);
executeCb = a2;
break;
case 3:
nodbUtil.assert(nodbUtil.isObjectOrArray(a2), 'NJS-005', 2);
nodbUtil.assert(typeof a3 === 'function', 'NJS-005', 3);
binds = a2;
executeCb = a3;
break;
case 4:
nodbUtil.assert(nodbUtil.isObjectOrArray(a2), 'NJS-005', 2);
nodbUtil.assert(nodbUtil.isObject(a3), 'NJS-005', 3);
nodbUtil.assert(typeof a4 === 'function', 'NJS-005', 4);
binds = a2;
executeOpts = a3;
executeCb = a4;
@ -177,7 +174,7 @@ function executeMany(sql, bindsOrNumIters, a3, a4) {
let executeCb;
let okBinds;
nodbUtil.assert(arguments.length > 2 && arguments.length < 5, 'NJS-009');
nodbUtil.checkAsyncArgs(arguments, 3, 4);
nodbUtil.assert(typeof sql === 'string', 'NJS-005', 1);
if (typeof bindsOrNumIters === 'number') {
nodbUtil.assert(Number.isInteger(bindsOrNumIters), 'NJS-005', 2);
@ -189,12 +186,10 @@ function executeMany(sql, bindsOrNumIters, a3, a4) {
switch (arguments.length) {
case 3:
nodbUtil.assert(typeof a3 === 'function', 'NJS-005', 3);
executeCb = a3;
break;
case 4:
nodbUtil.assert(nodbUtil.isObject(a3), 'NJS-005', 3);
nodbUtil.assert(typeof a4 === 'function', 'NJS-005', 4);
options = a3;
executeCb = a4;
break;
@ -211,9 +206,8 @@ function executeMany(sql, bindsOrNumIters, a3, a4) {
// first, but if not found, the database is queried and the result is cached
// using the fully qualified name
function getDbObjectClass(name, cb) {
nodbUtil.assert(arguments.length === 2, 'NJS-009');
nodbUtil.checkAsyncArgs(arguments, 2, 2);
nodbUtil.assert(typeof name === 'string', 'NJS-005', 1);
nodbUtil.assert(typeof cb === 'function', 'NJS-005', 2);
// check the cache; if the class is found there, nothing further to do
let cls = this._dbObjectClasses[name];
@ -231,8 +225,7 @@ function getDbObjectClass(name, cb) {
function getStatementInfo(sql, getStatementInfoCb) {
const self = this;
nodbUtil.assert(arguments.length === 2, 'NJS-009');
nodbUtil.assert(typeof getStatementInfoCb === 'function', 'NJS-005', 1);
nodbUtil.checkAsyncArgs(arguments, 2, 2);
self._getStatementInfo.apply(self, arguments);
}
@ -241,9 +234,7 @@ function getStatementInfo(sql, getStatementInfoCb) {
function commit(commitCb) {
const self = this;
nodbUtil.assert(arguments.length === 1, 'NJS-009');
nodbUtil.assert(typeof commitCb === 'function', 'NJS-005', 1);
nodbUtil.checkAsyncArgs(arguments, 1, 1);
self._commit.apply(self, arguments);
}
@ -251,8 +242,7 @@ function commit(commitCb) {
function createLob(type, createLobCb) {
const self = this;
nodbUtil.assert(arguments.length === 2, 'NJS-009');
nodbUtil.assert(typeof createLobCb === 'function', 'NJS-005', 2);
nodbUtil.checkAsyncArgs(arguments, 2, 2);
self._createLob.apply(self, arguments);
}
@ -261,8 +251,7 @@ function createLob(type, createLobCb) {
function rollback(rollbackCb) {
const self = this;
nodbUtil.assert(arguments.length === 1, 'NJS-009');
nodbUtil.assert(typeof rollbackCb === 'function', 'NJS-005', 1);
nodbUtil.checkAsyncArgs(arguments, 1, 1);
self._rollback.apply(self, arguments);
}
@ -274,16 +263,14 @@ function close(a1, a2) {
let options = {};
let closeCb;
nodbUtil.assert(arguments.length >= 1 && arguments.length <= 2, 'NJS-009');
nodbUtil.checkAsyncArgs(arguments, 1, 2);
switch (arguments.length) {
case 1:
nodbUtil.assert(typeof a1 === 'function', 'NJS-005', 1);
closeCb = a1;
break;
case 2:
nodbUtil.assert(nodbUtil.isObject(a1), 'NJS-005', 1);
nodbUtil.assert(typeof a2 === 'function', 'NJS-005', 2);
options = a1;
closeCb = a2;
break;
@ -307,8 +294,7 @@ function close(a1, a2) {
module.break = function(breakCb) {
const self = this;
nodbUtil.assert(arguments.length === 1, 'NJS-009');
nodbUtil.assert(typeof breakCb === 'function', 'NJS-005', 1);
nodbUtil.checkAsyncArgs(arguments, 1, 1);
self._break.apply(self, arguments);
};
@ -317,7 +303,7 @@ module.break = function(breakCb) {
function changePassword(user, password, newPassword, changePasswordCb) {
const self = this;
nodbUtil.assert(arguments.length === 4, 'NJS-009');
nodbUtil.checkAsyncArgs(arguments, 4, 4);
nodbUtil.assert(typeof user === 'string', 'NJS-005', 1);
nodbUtil.assert(typeof password === 'string', 'NJS-005', 2);
nodbUtil.assert(typeof newPassword === 'string', 'NJS-005', 3);
@ -330,16 +316,15 @@ function changePassword(user, password, newPassword, changePasswordCb) {
function getQueue(name, a2, a3) {
let options = {};
let queueCb;
nodbUtil.assert(arguments.length >= 2 && arguments.length <= 3, 'NJS-009');
nodbUtil.checkAsyncArgs(arguments, 2, 3);
nodbUtil.assert(typeof name === 'string', 'NJS-005', 1);
switch (arguments.length) {
case 2:
nodbUtil.assert(typeof a2 === 'function', 'NJS-005', 2);
queueCb = a2;
break;
case 3:
nodbUtil.assert(nodbUtil.isObject(a2), 'NJS-005', 2);
nodbUtil.assert(typeof a3 === 'function', 'NJS-005', 3);
options = a2;
queueCb = a3;
break;
@ -351,9 +336,7 @@ function getQueue(name, a2, a3) {
function ping(pingCb) {
const self = this;
nodbUtil.assert(arguments.length === 1, 'NJS-009');
nodbUtil.assert(typeof pingCb === 'function', 'NJS-005', 1);
nodbUtil.checkAsyncArgs(arguments, 1, 1);
self._ping.apply(self, arguments);
}
@ -362,10 +345,9 @@ function ping(pingCb) {
function subscribe(name, options, subscribeCb) {
const self = this;
nodbUtil.assert(arguments.length == 3, 'NJS-009');
nodbUtil.checkAsyncArgs(arguments, 3, 3);
nodbUtil.assert(typeof name === 'string', 'NJS-005', 1);
nodbUtil.assert(nodbUtil.isObject(options), 'NJS-005', 2);
nodbUtil.assert(typeof subscribeCb === 'function', 'NJS-005', 3);
self._subscribe.call(self, name, options, subscribeCb);
}
@ -373,9 +355,8 @@ function subscribe(name, options, subscribeCb) {
function unsubscribe(name, cb) {
const self = this;
nodbUtil.assert(arguments.length == 2, 'NJS-009');
nodbUtil.checkAsyncArgs(arguments, 2, 2);
nodbUtil.assert(typeof name === 'string', 'NJS-005', 1);
nodbUtil.assert(typeof cb === 'function', 'NJS-005', 2);
self._unsubscribe.call(self, name, cb);
}
@ -447,7 +428,7 @@ class Connection extends EventEmitter {
// To obtain a SodaDatabase object (high-level SODA object associated with
// current connection)
getSodaDatabase() {
nodbUtil.assert(arguments.length === 0, 'NJS-009');
nodbUtil.checkArgCount(arguments, 0, 0);
return this._getSodaDatabase();
}
@ -457,7 +438,7 @@ class Connection extends EventEmitter {
const self = this;
let stream;
nodbUtil.assert(arguments.length > 0 && arguments.length < 4, 'NJS-009');
nodbUtil.checkArgCount(arguments, 1, 3);
nodbUtil.assert(typeof sql === 'string', 'NJS-005', 1);
if (binding) {

View File

@ -24,8 +24,7 @@ const nodbUtil = require('./util.js');
const util = require('util');
function close(closeCb) {
nodbUtil.assert(arguments.length === 1, 'NJS-009');
nodbUtil.assert(typeof closeCb === 'function', 'NJS-005', 1);
nodbUtil.checkAsyncArgs(arguments, 1, 1);
// Return if LOB already closed to support multiple close() calls should be
// no-op
@ -47,8 +46,7 @@ function close(closeCb) {
function getData(getDataCb) {
nodbUtil.assert(arguments.length === 1, 'NJS-009');
nodbUtil.assert(typeof getDataCb === 'function', 'NJS-005', 1);
nodbUtil.checkAsyncArgs(arguments, 1, 1);
this._getData(getDataCb);
}

View File

@ -98,7 +98,7 @@ class OracleDb {
getPool(poolAlias) {
let pool;
nodbUtil.assert(arguments.length < 2, 'NJS-009');
nodbUtil.checkArgCount(arguments, 0, 1);
if (poolAlias) {
nodbUtil.assert(typeof poolAlias === 'string' || typeof poolAlias === 'number', 'NJS-005', 1);
@ -133,9 +133,8 @@ function createPool(poolAttrs, createPoolCb) {
// Initial argument count and type checks are done first and throw in the same
// call stack.
nodbUtil.assert(arguments.length === 2, 'NJS-009');
nodbUtil.checkAsyncArgs(arguments, 2, 2);
nodbUtil.assert(nodbUtil.isObject(poolAttrs), 'NJS-005', 1);
nodbUtil.assert(typeof createPoolCb === 'function', 'NJS-005', 2);
// Additional validations should pass errors via the callback. Need to ensure
// that errors are raised prior to actually creating the pool via _createPool.
@ -224,21 +223,17 @@ function getConnection(a1, a2) {
let connAttrs = {};
let getConnectionCb;
nodbUtil.assert(arguments.length < 3, 'NJS-009');
nodbUtil.checkAsyncArgs(arguments, 1, 2);
// Verify the number and types of arguments, then initialize the local poolAlias,
// connAttrs, and getConnectionCb variables based on the arguments.
switch (arguments.length) {
case 1:
nodbUtil.assert(typeof a1 === 'function', 'NJS-005', 1);
poolAlias = defaultPoolAlias;
getConnectionCb = a1;
break;
case 2:
nodbUtil.assert(typeof a1 === 'string' || nodbUtil.isObject(a1), 'NJS-005', 1);
nodbUtil.assert(typeof a2 === 'function', 'NJS-005', 2);
if (typeof a1 === 'string') {
poolAlias = a1;

View File

@ -156,7 +156,7 @@ function getConnection(a1, a2) {
let getConnectionCb;
let options = {};
nodbUtil.assert(arguments.length >= 1 && arguments.length <= 2, 'NJS-009');
nodbUtil.checkAsyncArgs(arguments, 1, 2);
switch (arguments.length) {
case 1:
getConnectionCb = a1;
@ -167,7 +167,6 @@ function getConnection(a1, a2) {
getConnectionCb = a2;
break;
}
nodbUtil.assert(typeof getConnectionCb === 'function', 'NJS-005', arguments.length);
if (self.status === self._oracledb.POOL_STATUS_DRAINING) { // closing soon
getConnectionCb(new Error(nodbUtil.getErrorMessage('NJS-064')));
@ -238,7 +237,7 @@ function close(a1, a2) {
let timeoutCb;
let closeCb;
nodbUtil.assert(arguments.length === 1 || arguments.length === 2, 'NJS-009');
nodbUtil.checkArgCount(arguments, 1, 2);
switch (arguments.length) {
case 1:

View File

@ -26,8 +26,7 @@ const nodbUtil = require('./util.js');
function close(closeCb) {
const self = this;
nodbUtil.assert(arguments.length === 1, 'NJS-009');
nodbUtil.assert(typeof closeCb === 'function', 'NJS-005', 1);
nodbUtil.checkAsyncArgs(arguments, 1, 1);
if (self._convertedToStream) {
closeCb(new Error(nodbUtil.getErrorMessage('NJS-042')));
@ -49,8 +48,7 @@ function close(closeCb) {
function getRow(getRowCb) {
const self = this;
nodbUtil.assert(arguments.length === 1, 'NJS-009');
nodbUtil.assert(typeof getRowCb === 'function', 'NJS-005', 1);
nodbUtil.checkAsyncArgs(arguments, 1, 1);
if (self._convertedToStream && !self._allowGetRowCall) {
getRowCb(new Error(nodbUtil.getErrorMessage('NJS-042')));
@ -91,10 +89,9 @@ function getRows(numRows, getRowsCb) {
const self = this;
let rowsNeeded;
nodbUtil.assert(arguments.length === 2, 'NJS-009');
nodbUtil.checkAsyncArgs(arguments, 2, 2);
nodbUtil.assert(Number.isInteger(numRows), 'NJS-005', 2);
nodbUtil.assert(numRows > 0, 'NJS-005', 2);
nodbUtil.assert(typeof getRowsCb === 'function', 'NJS-005', 2);
if (self._convertedToStream) {
getRowsCb(new Error(nodbUtil.getErrorMessage('NJS-042')));
@ -160,7 +157,7 @@ class ResultSet {
toQueryStream() {
const self = this;
nodbUtil.assert(arguments.length === 0, 'NJS-009');
nodbUtil.checkArgCount(arguments, 0, 0);
if (self._processingStarted) {
throw new Error(nodbUtil.getErrorMessage('NJS-041'));

View File

@ -23,18 +23,16 @@ const nodbUtil = require('./util.js');
// To drop the collection
function drop(cb) {
nodbUtil.assert(arguments.length === 1, 'NJS-009');
nodbUtil.assert(typeof cb === 'function', 'NJS-005', 1);
nodbUtil.checkAsyncArgs(arguments, 1, 1);
this._drop(cb);
}
// To insert a given document to the current collection
function insertMany(docs, cb) {
nodbUtil.assert(arguments.length === 2, 'NJS-009');
nodbUtil.checkAsyncArgs(arguments, 2, 2);
nodbUtil.assert(Array.isArray(docs), 'NJS-005', 1);
nodbUtil.assert(typeof cb === 'function', 'NJS-005', 2);
if (docs.length == 0) {
cb(new Error(nodbUtil.getErrorMessage('NJS-005', 1)));
return;
@ -56,9 +54,9 @@ function insertMany(docs, cb) {
// To insert a given document into the current collection and return the
// metadata of the new document (result-document)
function insertManyAndGet(docs, cb) {
nodbUtil.assert(arguments.length === 2, 'NJS-009');
nodbUtil.checkAsyncArgs(arguments, 2, 2);
nodbUtil.assert(Array.isArray(docs), 'NJS-005', 1);
nodbUtil.assert(typeof cb === 'function', 'NJS-005', 2);
if (docs.length == 0) {
cb(new Error(nodbUtil.getErrorMessage('NJS-005', 1)));
return;
@ -78,10 +76,9 @@ function insertManyAndGet(docs, cb) {
// To insert a given document to the current collection
function insertOne(content, cb) {
nodbUtil.assert(arguments.length === 2, 'NJS-009');
nodbUtil.checkAsyncArgs(arguments, 2, 2);
nodbUtil.assert(nodbUtil.isObject(content) ||
nodbUtil.isSodaDocument(content), 'NJS-005', 1);
nodbUtil.assert(typeof cb === 'function', 'NJS-005', 2);
if (!nodbUtil.isSodaDocument(content)) {
content = Buffer.from(JSON.stringify(content));
@ -94,10 +91,9 @@ function insertOne(content, cb) {
// To insert a given document into the current collection and return the
// metadata of the new document (result-document)
function insertOneAndGet(content, cb) {
nodbUtil.assert(arguments.length === 2, 'NJS-009');
nodbUtil.checkAsyncArgs(arguments, 2, 2);
nodbUtil.assert(nodbUtil.isObject(content) ||
nodbUtil.isSodaDocument(content), 'NJS-005', 1);
nodbUtil.assert(typeof cb === 'function', 'NJS-005', 2);
if (!nodbUtil.isSodaDocument(content)) {
content = Buffer.from(JSON.stringify(content));
@ -109,9 +105,8 @@ function insertOneAndGet(content, cb) {
// To create an index on documens
function createIndex(spec, cb) {
nodbUtil.assert(arguments.length === 2, 'NJS-009', 1);
nodbUtil.checkAsyncArgs(arguments, 2, 2);
nodbUtil.assert(nodbUtil.isObject(spec), 'NJS-005', 1);
nodbUtil.assert(typeof cb === 'function', 'NJS-005', 2);
this._createIndex(JSON.stringify(spec), cb);
}
@ -122,17 +117,15 @@ function dropIndex(indexName, a2, a3) {
let options = {};
let dropIndexCb;
nodbUtil.assert(arguments.length >= 2 && arguments.length <= 3, 'NJS-009');
nodbUtil.checkAsyncArgs(arguments, 2, 3);
nodbUtil.assert(typeof indexName === 'string', 'NJS-005', 1);
switch (arguments.length) {
case 2:
nodbUtil.assert(typeof a2 === 'function', 'NJS-005', 2);
dropIndexCb = a2;
break;
case 3:
nodbUtil.assert(typeof a2 === 'object', 'NJS-005', 2);
nodbUtil.assert(typeof a3 === 'function', 'NJS-005', 3);
options = a2;
dropIndexCb = a3;
break;
@ -144,8 +137,7 @@ function dropIndex(indexName, a2, a3) {
// To obtain the dataGuide of the document collection
function getDataGuide(cb) {
nodbUtil.assert(typeof cb === 'function', 'NJS-009');
nodbUtil.checkAsyncArgs(arguments, 1, 1);
this._getDataGuide(cb);
}
@ -164,7 +156,7 @@ class SodaCollection {
}
find() {
nodbUtil.assert(arguments.length === 0, 'NJS-009');
nodbUtil.checkArgCount(arguments, 0, 0 );
return this._find();
}

View File

@ -26,17 +26,15 @@ function createCollection(name, a2, a3) {
let options = {};
let createCollCb;
nodbUtil.assert(arguments.length >= 2 && arguments.length <= 3, 'NJS-009');
nodbUtil.checkAsyncArgs(arguments, 2, 3);
nodbUtil.assert(typeof name === 'string', 'NJS-005', 1);
switch (arguments.length) {
case 2:
nodbUtil.assert(typeof a2 === 'function', 'NJS-005', 2);
createCollCb = a2;
break;
case 3:
nodbUtil.assert(nodbUtil.isObject(a2), 'NJS-005', 2);
nodbUtil.assert(typeof a3 === 'function', 'NJS-005', 3);
options = a2;
createCollCb = a3;
if (options.metaData) {
@ -56,9 +54,8 @@ function createCollection(name, a2, a3) {
// To open a collection using given name
// if the collection does not exist, undefined is returned
function openCollection(name, openCollCb) {
nodbUtil.assert(arguments.length === 2, 'NJS-009');
nodbUtil.checkAsyncArgs(arguments, 2, 2);
nodbUtil.assert(typeof name === 'string', 'NJS-005', 1);
nodbUtil.assert(typeof openCollCb === 'function', 'NJS-005', 2);
this._openCollection(name, openCollCb);
}
@ -69,16 +66,14 @@ function getCollectionNames(a1, a2) {
let options = {};
let getCollNamesCb;
nodbUtil.assert(arguments.length >= 1 && arguments.length <= 2, 'NJS-009');
nodbUtil.checkAsyncArgs(arguments, 1, 2);
switch(arguments.length) {
case 1:
nodbUtil.assert(typeof a1 === 'function', 'NJS-005', 1);
getCollNamesCb = a1;
break;
case 2:
nodbUtil.assert(nodbUtil.isObject(a1), 'NJS-005', 1);
nodbUtil.assert(typeof a2 === 'function', 'NJS-005', 2);
options = a1;
getCollNamesCb = a2;
break;
@ -102,7 +97,7 @@ class SodaDatabase {
createDocument(content, a2) {
let options = {};
nodbUtil.assert(arguments.length >= 1 && arguments.length <= 2, 'NJS-009');
nodbUtil.checkArgCount(arguments, 1, 2);
nodbUtil.assert(Buffer.isBuffer(content) || typeof content === 'string' ||
nodbUtil.isObject(content), 'NJS-005', 1);
if (arguments.length > 1) {

View File

@ -26,8 +26,7 @@ const nodbUtil = require('./util.js');
// To obtain the next document from the cursor
//-----------------------------------------------------------------------------
function getNext(cb) {
nodbUtil.assert(arguments.length === 1, 'NJS-009');
nodbUtil.assert(typeof cb === 'function', 'NJS-005', 1);
nodbUtil.checkAsyncArgs(arguments, 1, 1);
this._getNext(cb);
}
@ -38,8 +37,7 @@ function getNext(cb) {
// to close an open cursor to clear of the resources allocated
//----------------------------------------------------------------------------
function close(cb) {
nodbUtil.assert(arguments.length === 1, 'NJS-009');
nodbUtil.assert(typeof cb === 'function', 'NJS-005', 1);
nodbUtil.checkAsyncArgs(arguments, 1, 1);
this._close(cb);
}

View File

@ -24,8 +24,7 @@ const nodbUtil = require('./util.js');
// to obtain count of documents from find() result
// This is a terminal function (no further chaining possible)
function count(cb) {
nodbUtil.assert(arguments.length === 1, 'NJS-009');
nodbUtil.assert(typeof cb === 'function', 'NJS-005', 1);
nodbUtil.checkAsyncArgs(arguments, 1, 1);
this._count(this._options, cb);
}
@ -34,8 +33,7 @@ function count(cb) {
// To obtain first document object from find() result
// This is a terminal function (no further chaining possible)
function getOne(cb) {
nodbUtil.assert(arguments.length === 1, 'NJS-009');
nodbUtil.assert(typeof cb === 'function', 'NJS-005', 1);
nodbUtil.checkAsyncArgs(arguments, 1, 1);
this._getOne(this._options, cb);
}
@ -44,11 +42,9 @@ function getOne(cb) {
// To replace one document by a given document based from find() result
// This is a terminal function (no further chaining possible)
function replaceOne(content, cb) {
nodbUtil.assert(arguments.length === 2, 'NJS-009');
nodbUtil.checkAsyncArgs(arguments, 2, 2);
nodbUtil.assert(nodbUtil.isObject(content) ||
nodbUtil.isSodaDocument(content), 'NJS-005', 1);
nodbUtil.assert(typeof cb === 'function', 'NJS-005', 2);
if (!nodbUtil.isSodaDocument(content)) {
content = Buffer.from(JSON.stringify(content));
@ -61,10 +57,9 @@ function replaceOne(content, cb) {
// To replace one document by a given document based from find() result
// This is a terminal function (no further chaining possible)
function replaceOneAndGet(content, cb) {
nodbUtil.assert(arguments.length === 2, 'NJS-009');
nodbUtil.checkAsyncArgs(arguments, 2, 2);
nodbUtil.assert(nodbUtil.isObject(content) ||
nodbUtil.isSodaDocument(content), 'NJS-005', 1);
nodbUtil.assert(typeof cb === 'function', 'NJS-005', 2);
if (!nodbUtil.isSodaDocument(content)) {
content = Buffer.from(JSON.stringify(content));
@ -77,8 +72,7 @@ function replaceOneAndGet(content, cb) {
// To remove documents obtained from find() result
// This is a terminal function (no further chaining possible)
function remove(cb) {
nodbUtil.assert(arguments.length === 1, 'NJS-009');
nodbUtil.assert(typeof cb === 'function', 'NJS-005', 1);
nodbUtil.checkAsyncArgs(arguments, 1, 1);
this._remove(this._options, cb);
}
@ -87,8 +81,7 @@ function remove(cb) {
// To obtain document-cursor object from find() result
// This is a terminal function (no further chaining possible)
function getCursor(cb) {
nodbUtil.assert(arguments.length === 1, 'NJS-009');
nodbUtil.assert(typeof cb === 'function', 'NJS-005', 1);
nodbUtil.checkAsyncArgs(arguments, 1, 1);
this._getCursor(this._options, cb);
}
@ -97,8 +90,7 @@ function getCursor(cb) {
// to obtain documents from find() result
// This is a terminal function (no further chaining possible)
function getDocuments(cb) {
nodbUtil.assert(arguments.length === 1, 'NJS-009');
nodbUtil.assert(typeof cb === 'function', 'NJS-005', 1);
nodbUtil.checkAsyncArgs(arguments, 1, 1);
this._getDocuments(this._options, cb);
}
@ -123,7 +115,7 @@ class SodaOperation {
// filter property - a non-terminal function and can chain further
filter(f) {
nodbUtil.assert(arguments.length === 1, 'NJS-009');
nodbUtil.checkArgCount (arguments, 1, 1);
nodbUtil.assert(nodbUtil.isObject(f), 'NJS-005', 1);
this._options.filter = JSON.stringify(f);
return this;
@ -131,7 +123,7 @@ class SodaOperation {
// key - a non-terminal function and can chain further
key(k) {
nodbUtil.assert(arguments.length === 1, 'NJS-009');
nodbUtil.checkArgCount(arguments, 1, 1);
nodbUtil.assert(typeof k === 'string', 'NJS-005', 1);
this._options.key = k;
this._options.keys = undefined;
@ -140,7 +132,7 @@ class SodaOperation {
// keys - a non-terminal function and can chain further
keys(arr) {
nodbUtil.assert(arguments.length === 1, 'NJS-009');
nodbUtil.checkArgCount(arguments, 1, 1);
nodbUtil.assert(Array.isArray(arr), 'NJS-005', 1);
for (let i = 0; i < arr.length; i++) {
@ -154,7 +146,7 @@ class SodaOperation {
// limit property - a non-terminal function and can chain further
limit (n) {
nodbUtil.assert(arguments.length === 1, 'NJS-009');
nodbUtil.checkArgCount(arguments, 1, 1);
nodbUtil.assert(typeof n === 'number', 'NJS-005', 1);
this._options.limit = n;
return this;
@ -162,7 +154,7 @@ class SodaOperation {
// skip property - a non-terminal function and can chain further
skip(n) {
nodbUtil.assert(arguments.length === 1, 'NJS-009');
nodbUtil.checkArgCount(arguments, 1, 1);
nodbUtil.assert(typeof n === 'number', 'NJS-005', 1);
this._options.skip = n;
return this;
@ -170,7 +162,7 @@ class SodaOperation {
// version property - a non-terminal function and can chain further
version(v) {
nodbUtil.assert(arguments.length === 1, 'NJS-009');
nodbUtil.checkArgCount(arguments, 1, 1);
nodbUtil.assert(typeof v === 'string', 'NJS-005', 1);
this._options.version = v;
return this;

View File

@ -48,7 +48,7 @@ const errorMessages = {
'NJS-002': 'NJS-002: invalid pool',
'NJS-004': 'NJS-004: invalid value for property %s',
'NJS-005': 'NJS-005: invalid value for parameter %d',
'NJS-009': 'NJS-009: invalid number of parameters',
'NJS-009': 'NJS-009: invalid number of parameters: %d passed but %d expected',
'NJS-037': 'NJS-037: incompatible type of value provided',
'NJS-040': 'NJS-040: connection request timeout. Request exceeded queueTimeout of %d',
'NJS-041': 'NJS-041: cannot convert ResultSet to QueryStream after invoking methods',
@ -60,7 +60,8 @@ const errorMessages = {
'NJS-064': 'NJS-064: connection pool is closing',
'NJS-065': 'NJS-065: connection pool was closed',
'NJS-067': 'NJS-067: a pre-built node-oracledb binary was not found for %s',
'NJS-069': 'NJS-069: node-oracledb %s requires Node.js %s or later'
'NJS-069': 'NJS-069: node-oracledb %s requires Node.js %s or later',
'NJS-076': 'NJS-076: invalid number of parameters: %d passed but %d to %d expected',
};
// getInstallURL returns a string with installation URL
@ -149,6 +150,34 @@ function assert(condition, errorCode, messageArg1) {
module.exports.assert = assert;
// checkArgCount is used to validate the number of arguments, particularly with
// optional parameters (range of number of parameters). If the number of
// arguments is not within the given range, an error is thrown.
function checkArgCount(args, minArgCount, maxArgCount) {
if (args.length < minArgCount || args.length > maxArgCount) {
if (minArgCount == maxArgCount) {
throw new Error(getErrorMessage('NJS-009', args.length, minArgCount));
} else {
throw new Error (getErrorMessage('NJS-076', args.length,
minArgCount, maxArgCount));
}
}
}
module.exports.checkArgCount = checkArgCount;
// checkAsyncArgs is used to validate number of arguments and last parameter
// is of type 'function'. This is used by all asynchronous functions where
// the last parameter is expected to be a function.
function checkAsyncArgs (args, minArgCount, maxArgCount) {
checkArgCount(args, minArgCount, maxArgCount);
assert(typeof args[args.length - 1] === 'function', 'NJS-005', args.length);
}
module.exports.checkAsyncArgs = checkAsyncArgs;
// The promisify function is used to wrap async methods to add optional promise
// support. If the last parameter passed to a method is a function, then it is
// assumed that the callback pattern is being used and the method is invoked as
@ -179,11 +208,12 @@ function promisify(oracledb, func) {
func.apply(self, args);
} catch (err) {
errorCode = err.message.substr(0, 7);
errorCode = err.message.substr(0, err.message.indexOf(':'));
// Check for invalid number or type of parameter(s) as they should be
// eagerly thrown.
if (errorCode === 'NJS-009' || errorCode === 'NJS-005') {
if (errorCode === 'NJS-009' || errorCode === 'NJS-005' ||
errorCode === 'NJS-076' ) {
// Throwing the error outside of the promise wrapper so that its not
// swallowed up as a rejection.
process.nextTick(function() {

View File

@ -162,7 +162,7 @@ static napi_value njsDbObject_copy(napi_env env, napi_callback_info info)
return NULL;
}
if (actualArgs != 0) {
njsUtils_throwError(env, errInvalidNumberOfParameters);
njsUtils_throwError(env, errInvalidNumberOfParameters, actualArgs, 0);
return NULL;
}
@ -1004,7 +1004,8 @@ static bool njsDbObject_validateArgs(napi_env env, napi_callback_info info,
NJS_CHECK_NAPI(env, napi_get_cb_info(env, info, &actualArgs, args,
&thisArg, &data))
if (actualArgs != numArgs)
return njsUtils_throwError(env, errInvalidNumberOfParameters);
return njsUtils_throwError(env, errInvalidNumberOfParameters,
actualArgs, numArgs);
// data will either be an attribute or a pointer to the global njsOracleDb
// instance

View File

@ -33,7 +33,7 @@ static const char *njsErrorMessages[] = {
"NJS-004: invalid value for property %s", // errInvalidPropertyValue
"NJS-005: invalid value for parameter %d", // errInvalidParameterValue
"NJS-007: invalid value for \"%s\" in parameter %d", // errInvalidPropertyValueInParam
"NJS-009: invalid number of parameters", // errInvalidNumberOfParameters
"NJS-009: invalid number of parameters: %d passed but %d expected", // errInvalidNumberOfParameters
"NJS-010: unsupported data type %d in column %u", // errUnsupportedDataType
"NJS-011: encountered bind value and type mismatch", // errBindValueAndTypeMismatch
"NJS-012: encountered invalid bind data type in parameter %d", // errInvalidBindDataType
@ -81,6 +81,8 @@ static const char *njsErrorMessages[] = {
"NJS-072: cannot convert from attribute \"%.*s\" of type \"%.*s\" to JavaScript value", // errConvertFromObjAttr
"NJS-073: cannot convert from JavaScript value to element of type %.*s", // errConvertToObjElement
"NJS-074: cannot convert from JavaScript value to attribute \"%.*s\" of type \"%.*s\"", // errConvertToObjAttr
"NJS-075: only one of connectString and connectionString can be used", // errDblConnectionString
"NJS-076: invalid number of parameters: %d passed but %d to %d expected", //errInvalidNumberOfParametersRange
};

View File

@ -44,7 +44,7 @@ static napi_value njsModule_externalInit(napi_env env, napi_callback_info info)
return NULL;
}
if (actualArgs != 1) {
njsUtils_throwError(env, errInvalidNumberOfParameters);
njsUtils_throwError(env, errInvalidNumberOfParameters, actualArgs, 1);
return NULL;
}

View File

@ -191,6 +191,9 @@ typedef enum {
errConvertFromObjAttr,
errConvertToObjElement,
errConvertToObjAttr,
errDblConnectionString,
errInvalidNumberOfParametersRange,
// New ones should be added here

View File

@ -413,7 +413,7 @@ static bool njsOracleDb_createPoolPostAsync(njsBaton *baton, napi_env env,
static bool njsOracleDb_createPoolProcessArgs(njsBaton *baton, napi_env env,
napi_value *args)
{
bool found;
bool connStrFound, connStrFound1;
// initialize ODPI-C library, if necessary
if (!njsOracleDb_initDPI(baton->oracleDb, env, baton))
@ -442,12 +442,14 @@ static bool njsOracleDb_createPoolProcessArgs(njsBaton *baton, napi_env env,
&baton->password, &baton->passwordLength, NULL))
return false;
if (!njsBaton_getStringFromArg(baton, env, args, 0, "connectString",
&baton->connectString, &baton->connectStringLength, &found))
&baton->connectString, &baton->connectStringLength, &connStrFound))
return false;
if (!found && !njsBaton_getStringFromArg(baton, env, args, 0,
"connectionString", &baton->connectString,
&baton->connectStringLength, NULL))
if (!njsBaton_getStringFromArg(baton, env, args, 0, "connectionString",
&baton->connectString, &baton->connectStringLength,
&connStrFound1))
return false;
if (connStrFound && connStrFound1)
return njsBaton_setError (baton, errDblConnectionString);
if (!njsBaton_getStringFromArg(baton, env, args, 0, "edition",
&baton->edition, &baton->editionLength, NULL))
return false;
@ -627,7 +629,7 @@ static bool njsOracleDb_getConnectionPostAsync(njsBaton *baton, napi_env env,
static bool njsOracleDb_getConnectionProcessArgs(njsBaton *baton,
napi_env env, napi_value *args)
{
bool found;
bool connStrFound, connStrFound1;
// initialize ODPI-C library, if necessary
if (!njsOracleDb_initDPI(baton->oracleDb, env, baton))
@ -654,13 +656,16 @@ static bool njsOracleDb_getConnectionProcessArgs(njsBaton *baton,
if (!njsBaton_getStringFromArg(baton, env, args, 0, "password",
&baton->password, &baton->passwordLength, NULL))
return false;
if (!njsBaton_getStringFromArg(baton, env, args, 0, "connectString",
&baton->connectString, &baton->connectStringLength, &found))
&baton->connectString, &baton->connectStringLength, &connStrFound))
return false;
if (!found && !njsBaton_getStringFromArg(baton, env, args, 0,
"connectionString", &baton->connectString,
&baton->connectStringLength, NULL))
if (!njsBaton_getStringFromArg(baton, env, args, 0, "connectionString",
&baton->connectString, &baton->connectStringLength,
&connStrFound1))
return false;
if (connStrFound && connStrFound1)
return njsBaton_setError (baton, errDblConnectionString );
if (!njsBaton_getStringFromArg(baton, env, args, 0, "newPassword",
&baton->newPassword, &baton->newPasswordLength, NULL))
return false;

View File

@ -855,7 +855,8 @@ bool njsUtils_validateArgs(napi_env env, napi_callback_info info,
NJS_CHECK_NAPI(env, napi_get_cb_info(env, info, &actualArgs, args,
&thisArg, NULL))
if (actualArgs != numArgs)
return njsUtils_throwError(env, errInvalidNumberOfParameters);
return njsUtils_throwError(env, errInvalidNumberOfParameters,
actualArgs, numArgs);
// unwrap instance
NJS_CHECK_NAPI(env, napi_unwrap(env, thisArg, (void**) instance))

View File

@ -941,4 +941,27 @@ describe('1. connection.js', function(){
}); // 1.9.2
}); // 1.9
describe('1.10 connectString & connectionString specified', function() {
it('1.10.1 both connectString & ConnectionString specified',
function (done) {
oracledb.getConnection(
{
user : dbConfig.user,
password : dbConfig.password,
connectString : dbConfig.connectString,
connectionString : dbConfig.connectString
},
function(err, conn) {
should.not.exist(conn);
should.exist(err);
(err.message).should.startWith('NJS-075:');
done();
}
);
}
); // 1.10.1
}); // 1.10
});

View File

@ -954,4 +954,29 @@ describe('2. pool.js', function() {
}); // 2.12
describe('2.13 connectString & connectionString provided', function() {
it('2.13.1 both connectString & connectionString provided',
function(done) {
oracledb.createPool(
{
user: dbConfig.user,
password: dbConfig.password,
connectString: dbConfig.connectString,
connectionString: dbConfig.connectString,
poolMin: 1,
poolMax: 1,
poolIncrement: 0
},
function(err, pool) {
should.not.exist(pool);
should.exist(err);
(err.message).should.startWith('NJS-075');
done();
}
);
}
); // 2.13.1
}); // 2.13.1
});

View File

@ -225,7 +225,7 @@ describe('14. stream2.js', function() {
function() {
connection.queryStream();
},
/NJS-009: invalid number of parameters/
/NJS-076: invalid number of parameters/
);
done();
});