From f7b1edc7bbc994427b59cae57237cc52c8d51950 Mon Sep 17 00:00:00 2001 From: Brian C Date: Sun, 18 Jun 2017 14:09:18 -0500 Subject: [PATCH] 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. --- index.js | 2 +- test/events.js | 16 ++++++++++++++++ 2 files changed, 17 insertions(+), 1 deletion(-) diff --git a/index.js b/index.js index 2e30ac6e..7e0f0ce5 100644 --- a/index.js +++ b/index.js @@ -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) { diff --git a/test/events.js b/test/events.js index bcb523f0..759dff02 100644 --- a/test/events.js +++ b/test/events.js @@ -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) {