Skip to content

Commit

Permalink
Fix early detection of bad callback to connection.query
Browse files Browse the repository at this point in the history
  • Loading branch information
dougwilson committed Jan 21, 2020
1 parent 1428049 commit ca84d0a
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 17 deletions.
33 changes: 16 additions & 17 deletions lib/Connection.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,38 +27,37 @@ Connection.createQuery = function createQuery(sql, values, callback) {
return sql;
}

var cb = wrapCallbackInDomain(null, callback);
var cb = callback;
var options = {};

if (typeof sql === 'function') {
cb = wrapCallbackInDomain(null, sql);
return new Query(options, cb);
}

if (typeof sql === 'object') {
cb = sql;
} else if (typeof sql === 'object') {
for (var prop in sql) {
options[prop] = sql[prop];
}

if (typeof values === 'function') {
cb = wrapCallbackInDomain(null, values);
cb = values;
} else if (values !== undefined) {
options.values = values;
}
} else {
options.sql = sql;
options.values = values;

return new Query(options, cb);
if (typeof values === 'function') {
cb = values;
options.values = undefined;
}
}

options.sql = sql;
options.values = values;

if (typeof values === 'function') {
cb = wrapCallbackInDomain(null, values);
options.values = undefined;
}
if (cb !== undefined) {
cb = wrapCallbackInDomain(null, cb);

if (cb === undefined && callback !== undefined) {
throw new TypeError('argument callback must be a function when provided');
if (cb === undefined) {
throw new TypeError('argument callback must be a function when provided');
}
}

return new Query(options, cb);
Expand Down
4 changes: 4 additions & 0 deletions test/unit/query/test-args-query-bad-callback.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,10 @@ server.listen(common.fakeServerPort, function (err) {
connection.query('SELECT ?', [1], 'oops');
}, /TypeError: argument callback must be a function when provided/);

assert.throws(function () {
connection.query({ sql: 'SELECT ?' }, [1], 'oops');
}, /TypeError: argument callback must be a function when provided/);

connection.destroy();
server.destroy();
});

0 comments on commit ca84d0a

Please sign in to comment.