diff --git a/lib/connection.js b/lib/connection.js index 2f79016..3c0a067 100644 --- a/lib/connection.js +++ b/lib/connection.js @@ -18,6 +18,7 @@ var Connection = function(config) { this.parsedStatements = {}; this.writer = new Writer(); this.ssl = config.ssl || false; + this._ending = false; }; util.inherits(Connection, EventEmitter); @@ -37,6 +38,11 @@ Connection.prototype.connect = function(port, host) { }); this.stream.on('error', function(error) { + //don't raise ECONNRESET errors - they can & should be ignored + //during disconnect + if(self._ending && error.code == 'ECONNRESET') { + return; + } self.emit('error', error); }); @@ -263,6 +269,7 @@ Connection.prototype.end = function() { //0x58 = 'X' this.writer.add(emptyBuffer); this._send(0x58); + this._ending = true; }; Connection.prototype.describe = function(msg, more) { diff --git a/test/unit/connection/error-tests.js b/test/unit/connection/error-tests.js index bccffac..98eb20a 100644 --- a/test/unit/connection/error-tests.js +++ b/test/unit/connection/error-tests.js @@ -1,10 +1,30 @@ var helper = require(__dirname + '/test-helper'); var Connection = require(__dirname + '/../../../lib/connection'); -var con = new Connection({stream: new MemoryStream()}); test("connection emits stream errors", function() { + var con = new Connection({stream: new MemoryStream()}); assert.emits(con, 'error', function(err) { assert.equal(err.message, "OMG!"); }); con.connect(); con.stream.emit('error', new Error("OMG!")); }); + +test('connection emits ECONNRESET errors during normal operation', function() { + var con = new Connection({stream: new MemoryStream()}); + con.connect(); + assert.emits(con, 'error', function(err) { + assert.equal(err.code, 'ECONNRESET'); + }); + var e = new Error('Connection Reset'); + e.code = 'ECONNRESET'; + con.stream.emit('error', e); +}); + +test('connection does not emit ECONNRESET errors during disconnect', function() { + var con = new Connection({stream: new MemoryStream()}); + con.connect(); + var e = new Error('Connection Reset'); + e.code = 'ECONNRESET'; + con.end(); + con.stream.emit('error', e); +});