Catch errors client throws in pool (#2569)

* Catch errors client throws in pool

* Add a test

This test _should be_ right
This commit is contained in:
Alex Zlotnik 2022-08-22 13:33:51 -07:00 committed by GitHub
parent 3e53d06cd8
commit 8032fbad43
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 29 additions and 14 deletions

View File

@ -406,20 +406,24 @@ class Pool extends EventEmitter {
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)
} else {
return cb(undefined, res)
}
})
try {
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)
} else {
return cb(undefined, res)
}
})
} catch (err) {
return cb(err)
}
})
return response.result
}

View File

@ -37,6 +37,17 @@ describe('pool error handling', function () {
})
})
it('Catches errors in client.query', async function () {
await expect((new Pool()).query(null)).to.throwError()
await expect(async () => {
try {
await (new Pool()).query(null)
} catch (e) {
console.log(e)
}
}).not.to.throwError()
})
describe('calling release more than once', () => {
it(
'should throw each time',