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
This commit is contained in:
jphaas 2016-08-26 10:08:15 -04:00 committed by Brian C
parent f2221a4040
commit b091cc0d05
2 changed files with 17 additions and 1 deletions

View File

@ -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)

View File

@ -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) {