Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Catch errors client throws in pool #2569

Merged
merged 2 commits into from Aug 22, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
32 changes: 18 additions & 14 deletions packages/pg-pool/index.js
Expand Up @@ -375,20 +375,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