Skip to content

Commit

Permalink
Fix error handling regression
Browse files Browse the repository at this point in the history
Fixes #180 #179
  • Loading branch information
voxpelli committed Aug 19, 2020
1 parent 7683d40 commit 5c324ac
Showing 1 changed file with 15 additions and 17 deletions.
32 changes: 15 additions & 17 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -240,32 +240,30 @@ module.exports = function (session) {
* @param {string} query - the database query to perform
* @param {any[]|PGStoreQueryCallback} [params] - the parameters of the query or the callback function
* @param {PGStoreQueryCallback} [fn] - standard Node.js callback returning the resulting rows
* @returns {Promise<PGStoreQueryResult>}
* @access private
*/
async query (query, params, fn) {
query (query, params, fn) {
if (typeof params === 'function') {
fn = fn || params;
if (fn) throw new Error('Two callback functions set at once');
fn = params;
params = [];
}

let result;

try {
if (this.pgPromise) {
const res = await this.pgPromise.query(query, params || []);
result = res && res[0] ? res[0] : false;
this.pgPromise.query(query, params || [])
.then(
/** @param {PGStoreQueryResult} res */
res => { fn && fn(null, res && res[0] ? res[0] : undefined); }
)
.catch(
/** @param {Error} err */
err => { fn && fn(err); }
);
} else {
const res = await this.pool.query(query, params || []);
result = res && res.rows[0] ? res.rows[0] : false;
}
} catch (err) {
errorToCallbackAndReject(err);
this.pool.query(query, params || [], function (err, res) {
if (fn) { fn(err, res && res.rows[0] ? res.rows[0] : undefined); }
});
}

if (fn) fn(undefined, result);

return result;
}

/**
Expand Down

0 comments on commit 5c324ac

Please sign in to comment.