Handle client errors in pool.query (#131)

If an error not related to the query occurs, the client is emitting an
error event.

Forward this event to the callback.
This commit is contained in:
Johannes Würbach 2019-12-18 17:08:30 +01:00 committed by Brian C
parent c8c41c5b65
commit 236db3813d
2 changed files with 33 additions and 0 deletions

View File

@ -316,13 +316,31 @@ class Pool extends EventEmitter {
}
const response = promisify(this.Promise, cb)
cb = response.callback
this.connect((err, client) => {
if (err) {
return cb(err)
}
let clientReleased = false
const onError = (err) => {
if (clientReleased) {
return
}
clientReleased = true
client.release(err)
cb(err)
}
client.once('error', onError)
this.log('dispatching query')
client.query(text, values, (err, res) => {
this.log('query dispatched')
client.removeListener('error', onError)
if (clientReleased) {
return
}
clientReleased = true
client.release(err)
if (err) {
return cb(err)

View File

@ -226,4 +226,19 @@ describe('pool error handling', function () {
})
})
})
it('handles post-checkout client failures in pool.query', (done) => {
const pool = new Pool({ max: 1 })
pool.on('error', () => {
// We double close the connection in this test, prevent exception caused by that
})
pool.query('SELECT pg_sleep(5)', [], (err) => {
expect(err).to.be.an(Error)
done()
})
setTimeout(() => {
pool._clients[0].end()
}, 1000)
})
})