Skip to content

Commit

Permalink
Add client to error event emitter (#65)
Browse files Browse the repository at this point in the history
When the pool emits an error pass the client as the 2nd parameter to the `on('error')` handler.
  • Loading branch information
brianc authored Jun 18, 2017
1 parent 5061068 commit f7b1edc
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 1 deletion.
2 changes: 1 addition & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ Pool.prototype._create = function (cb) {
this.log('connected client error:', e)
this.pool.destroy(client)
e.client = client
this.emit('error', e)
this.emit('error', e, client)
}.bind(this))

client.connect(function (err) {
Expand Down
16 changes: 16 additions & 0 deletions test/events.js
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,22 @@ describe('events', function () {
pool.end(done)
}, 40)
})

it('emits error and client if an idle client in the pool hits an error', function (done) {
var pool = new Pool()
pool.connect(function (err, client) {
expect(err).to.equal(null)
client.release()
setImmediate(function () {
client.emit('error', new Error('problem'))
})
pool.once('error', function (err, errClient) {
expect(err.message).to.equal('problem')
expect(errClient).to.equal(client)
done()
})
})
})
})

function mockClient (methods) {
Expand Down

0 comments on commit f7b1edc

Please sign in to comment.