Skip to content

Commit

Permalink
fix: remove unnecessary unhandled error warnings (#322)
Browse files Browse the repository at this point in the history
  • Loading branch information
luin authored Jun 22, 2016
1 parent 2ac00c8 commit a1ff2f6
Show file tree
Hide file tree
Showing 8 changed files with 50 additions and 12 deletions.
4 changes: 4 additions & 0 deletions lib/cluster/connection_pool.js
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,10 @@ ConnectionPool.prototype.findOrCreate = function (node, readOnly) {
});

this.emit('+node', redis);

redis.on('error', function (error) {
_this.emit('nodeError', error);
});
}

return redis;
Expand Down
7 changes: 5 additions & 2 deletions lib/cluster/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,9 @@ function Cluster(startupNodes, options) {
this.connectionPool.on('drain', function () {
_this.setStatus('close');
});
this.connectionPool.on('nodeError', function (error) {
_this.emit('node error', error);
});

this.slots = [];
this.retryAttempts = 0;
Expand Down Expand Up @@ -364,7 +367,7 @@ Cluster.prototype.executeOfflineCommands = function () {

Cluster.prototype.sendCommand = function (command, stream, node) {
if (this.status === 'end') {
command.reject(new Error('Connection is closed.'));
command.reject(new Error(utils.CONNECTION_CLOSED_ERROR_MSG));
return command.promise;
}
var to = this.options.scaleReads;
Expand Down Expand Up @@ -506,7 +509,7 @@ Cluster.prototype.handleError = function (error, ttl, handlers) {
timeout: this.options.retryDelayOnClusterDown,
callback: this.refreshSlotsCache.bind(this)
});
} else if (error.message === 'Connection is closed.' && this.options.retryDelayOnFailover > 0) {
} else if (error.message === utils.CONNECTION_CLOSED_ERROR_MSG && this.options.retryDelayOnFailover > 0) {
this.delayQueue.push('failover', handlers.connectionClosed, {
timeout: this.options.retryDelayOnFailover,
callback: this.refreshSlotsCache.bind(this)
Expand Down
3 changes: 2 additions & 1 deletion lib/connectors/connector.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
var _ = require('lodash');
var net = require('net');
var tls = require('tls');
var utils = require('../utils');

function Connector(options) {
this.options = options;
Expand Down Expand Up @@ -34,7 +35,7 @@ Connector.prototype.connect = function (callback) {
var _this = this;
process.nextTick(function () {
if (!_this.connecting) {
callback(new Error('Connection is closed.'));
callback(new Error(utils.CONNECTION_CLOSED_ERROR_MSG));
return;
}
var stream;
Expand Down
7 changes: 6 additions & 1 deletion lib/connectors/sentinel_connector.js
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ SentinelConnector.prototype.connect = function (callback) {
var endpoint = _this.sentinels[_this.currentPoint];
_this.resolve(endpoint, function (err, resolved) {
if (!_this.connecting) {
callback(new Error('Connection is closed.'));
callback(new Error(utils.CONNECTION_CLOSED_ERROR_MSG));
return;
}
if (resolved) {
Expand Down Expand Up @@ -166,11 +166,16 @@ SentinelConnector.prototype.resolve = function (endpoint, callback) {
dropBufferSupport: true
});

// ignore the errors since resolve* methods will handle them
client.on('error', noop);

if (this.options.role === 'slave') {
this.resolveSlave(client, callback);
} else {
this.resolveMaster(client, callback);
}
};

function noop() {}

module.exports = SentinelConnector;
31 changes: 25 additions & 6 deletions lib/redis.js
Original file line number Diff line number Diff line change
Expand Up @@ -438,14 +438,33 @@ Redis.prototype._readyCheck = function (callback) {
* @private
*/
Redis.prototype.silentEmit = function (eventName) {
var error;
if (eventName === 'error') {
error = arguments[1];

if (this.status === 'end') {
return;
}

if (this.manuallyClosing) {
// ignore connection related errors when manually disconnecting
if (
error instanceof Error &&
(
error.message === utils.CONNECTION_CLOSED_ERROR_MSG ||
error.syscall === 'connect' ||
error.syscall === 'read'
)
) {
return;
}
}
}
if (this.listeners(eventName).length > 0) {
return this.emit.apply(this, arguments);
}
if (eventName === 'error') {
var error = arguments[1];
if (error instanceof Error) {
console.error('[ioredis] Unhandled error event:', error.stack);
}
if (error && error instanceof Error) {
console.error('[ioredis] Unhandled error event:', error.stack);
}
return false;
};
Expand Down Expand Up @@ -528,7 +547,7 @@ Redis.prototype.sendCommand = function (command, stream) {
this.connect().catch(function () {});
}
if (this.status === 'end') {
command.reject(new Error('Connection is closed.'));
command.reject(new Error(utils.CONNECTION_CLOSED_ERROR_MSG));
return command.promise;
}
if (this.condition.subscriber && !Command.checkFlag('VALID_IN_SUBSCRIBER_MODE', command.name)) {
Expand Down
3 changes: 2 additions & 1 deletion lib/redis/event_handler.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

var debug = require('debug')('ioredis:connection');
var Command = require('../command');
var utils = require('../utils');

exports.connectHandler = function (self) {
return function () {
Expand Down Expand Up @@ -96,7 +97,7 @@ exports.closeHandler = function (self) {

function close() {
self.setStatus('end');
self.flushQueue(new Error('Connection is closed.'));
self.flushQueue(new Error(utils.CONNECTION_CLOSED_ERROR_MSG));
}
};

Expand Down
5 changes: 5 additions & 0 deletions lib/utils/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -295,3 +295,8 @@ exports.sample = function (array, from) {
}
return array[from + Math.floor(Math.random() * (length - from))];
};

/**
* Error message for connection being disconnected
*/
exports.CONNECTION_CLOSED_ERROR_MSG = 'Connection is closed.';
2 changes: 1 addition & 1 deletion test/functional/connection.js
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ describe('connection', function () {
});

var redis2 = new Redis(6390, { lazyConnect: true, retryStrategy: null });
redis2.connect().catch(function (err) {
redis2.connect().catch(function () {
if (!--pending) {
redis2.disconnect();
done();
Expand Down

0 comments on commit a1ff2f6

Please sign in to comment.