Skip to content

Commit

Permalink
Catch errors client throws in pool (#2569)
Browse files Browse the repository at this point in the history
* Catch errors client throws in pool

* Add a test

This test _should be_ right
  • Loading branch information
zlotnika committed Aug 22, 2022
1 parent 3e53d06 commit 8032fba
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 14 deletions.
32 changes: 18 additions & 14 deletions packages/pg-pool/index.js
Expand Up @@ -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
}
Expand Down
11 changes: 11 additions & 0 deletions packages/pg-pool/test/error-handling.js
Expand Up @@ -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',
Expand Down

0 comments on commit 8032fba

Please sign in to comment.