diff --git a/lib/connection.js b/lib/connection.js index 61f2562f..7b7a8abf 100644 --- a/lib/connection.js +++ b/lib/connection.js @@ -62,14 +62,15 @@ Connection.prototype.connect = function (port, host) { self.emit('connect') }) - this.stream.on('error', function (error) { + const reportStreamError = 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) - }) + } + this.stream.on('error', reportStreamError) this.stream.on('close', function () { self.emit('end') @@ -97,15 +98,7 @@ Connection.prototype.connect = function (port, host) { NPNProtocols: self.ssl.NPNProtocols }) self.attachListeners(self.stream) - - self.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) - }) + self.stream.on('error', reportStreamError) self.emit('sslconnect') })