mirror of
https://github.com/brianc/node-postgres.git
synced 2026-01-18 15:55:05 +00:00
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:
parent
c8c41c5b65
commit
236db3813d
18
index.js
18
index.js
@ -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)
|
||||
|
||||
@ -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)
|
||||
})
|
||||
})
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user