From b091cc0d057d1a146333ee6d5e5e8ca376ef9f2b Mon Sep 17 00:00:00 2001 From: jphaas Date: Fri, 26 Aug 2016 10:08:15 -0400 Subject: [PATCH] Bug fix: Pool.query now calls cb if connect() fails (#25) * Pool.query calls cb if connect() fails Old behavior was that if connect called back with an error, the promise would get rejected but the cb function would never get called. * Test that Pool.query passes connection errors to callback * Fixes to standardjs compliance --- index.js | 7 ++++++- test/index.js | 11 +++++++++++ 2 files changed, 17 insertions(+), 1 deletion(-) diff --git a/index.js b/index.js index 80279db2..10d8dd01 100644 --- a/index.js +++ b/index.js @@ -96,7 +96,12 @@ Pool.prototype.query = function (text, values, cb) { return new this.Promise(function (resolve, reject) { this.connect(function (err, client, done) { - if (err) return reject(err) + if (err) { + if (cb) { + cb(err) + } + return reject(err) + } client.query(text, values, function (err, res) { done(err) err ? reject(err) : resolve(res) diff --git a/test/index.js b/test/index.js index e4616a43..d7752fce 100644 --- a/test/index.js +++ b/test/index.js @@ -62,6 +62,17 @@ describe('pool', function () { }) }) + it('passes connection errors to callback', function (done) { + var pool = new Pool({host: 'no-postgres-server-here.com'}) + pool.query('SELECT $1::text as name', ['brianc'], function (err, res) { + expect(res).to.be(undefined) + expect(err).to.be.an(Error) + pool.end(function (err) { + done(err) + }) + }) + }) + it('removes client if it errors in background', function (done) { var pool = new Pool() pool.connect(function (err, client, release) {