Skip to content

Commit b091cc0

Browse files
jphaasbrianc
authored andcommitted
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
1 parent f2221a4 commit b091cc0

File tree

2 files changed

+17
-1
lines changed

2 files changed

+17
-1
lines changed

Diff for: index.js

+6-1
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,12 @@ Pool.prototype.query = function (text, values, cb) {
9696

9797
return new this.Promise(function (resolve, reject) {
9898
this.connect(function (err, client, done) {
99-
if (err) return reject(err)
99+
if (err) {
100+
if (cb) {
101+
cb(err)
102+
}
103+
return reject(err)
104+
}
100105
client.query(text, values, function (err, res) {
101106
done(err)
102107
err ? reject(err) : resolve(res)

Diff for: test/index.js

+11
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,17 @@ describe('pool', function () {
6262
})
6363
})
6464

65+
it('passes connection errors to callback', function (done) {
66+
var pool = new Pool({host: 'no-postgres-server-here.com'})
67+
pool.query('SELECT $1::text as name', ['brianc'], function (err, res) {
68+
expect(res).to.be(undefined)
69+
expect(err).to.be.an(Error)
70+
pool.end(function (err) {
71+
done(err)
72+
})
73+
})
74+
})
75+
6576
it('removes client if it errors in background', function (done) {
6677
var pool = new Pool()
6778
pool.connect(function (err, client, release) {

0 commit comments

Comments
 (0)