Skip to content

Commit f7b1edc

Browse files
authored
Add client to error event emitter (#65)
When the pool emits an error pass the client as the 2nd parameter to the `on('error')` handler.
1 parent 5061068 commit f7b1edc

File tree

2 files changed

+17
-1
lines changed

2 files changed

+17
-1
lines changed

index.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ Pool.prototype._create = function (cb) {
9090
this.log('connected client error:', e)
9191
this.pool.destroy(client)
9292
e.client = client
93-
this.emit('error', e)
93+
this.emit('error', e, client)
9494
}.bind(this))
9595

9696
client.connect(function (err) {

test/events.js

+16
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,22 @@ describe('events', function () {
6060
pool.end(done)
6161
}, 40)
6262
})
63+
64+
it('emits error and client if an idle client in the pool hits an error', function (done) {
65+
var pool = new Pool()
66+
pool.connect(function (err, client) {
67+
expect(err).to.equal(null)
68+
client.release()
69+
setImmediate(function () {
70+
client.emit('error', new Error('problem'))
71+
})
72+
pool.once('error', function (err, errClient) {
73+
expect(err.message).to.equal('problem')
74+
expect(errClient).to.equal(client)
75+
done()
76+
})
77+
})
78+
})
6379
})
6480

6581
function mockClient (methods) {

0 commit comments

Comments
 (0)