Skip to content

Commit

Permalink
fix(sentinel): improve the error message when connection to sentinel …
Browse files Browse the repository at this point in the history
…is rejected

Closes #280
  • Loading branch information
luin committed Apr 10, 2016
1 parent 91998e3 commit 3ca30d8
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 3 deletions.
11 changes: 9 additions & 2 deletions lib/connectors/sentinel_connector.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ SentinelConnector.prototype.connect = function (callback) {
}

var _this = this;
var lastError;
connectToNext();

function connectToNext() {
Expand All @@ -50,7 +51,11 @@ SentinelConnector.prototype.connect = function (callback) {
}
if (typeof retryDelay !== 'number') {
debug('All sentinels are unreachable and retry is disabled, emitting error...');
return callback(new Error('All sentinels are unreachable.'));
var error = 'All sentinels are unreachable.';
if (lastError) {
error += ' Last error: ' + lastError.message;
}
return callback(new Error(error));
}
debug('All sentinels are unreachable. Retrying from scratch after %d', retryDelay);
setTimeout(connectToNext, retryDelay);
Expand All @@ -68,9 +73,11 @@ SentinelConnector.prototype.connect = function (callback) {
callback(null, _this.stream);
} else if (err) {
debug('failed to connect to sentinel %s:%s because %s', endpoint.host, endpoint.port, err);
lastError = err;
connectToNext();
} else {
debug('connected to sentinel %s:%s successfully, but got a invalid reply: %s', endpoint.host, endpoint.port, resolved);
debug('connected to sentinel %s:%s successfully, but got a invalid reply: %s',
endpoint.host, endpoint.port, resolved);
connectToNext();
}
});
Expand Down
2 changes: 1 addition & 1 deletion lib/redis/parser.js
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,7 @@ exports.returnReply = function (reply) {
item = this.commandQueue.shift();
if (!item) {
return this.emit('error',
new Error('Command queue state error. If you can reproduce this, please report it.' + reply.toString()));
new Error('Command queue state error. If you can reproduce this, please report it. Last reply: ' + reply.toString()));
}
if (_.includes(Command.FLAGS.ENTER_SUBSCRIBER_MODE, item.command.name)) {
this.condition.subscriber = new SubscriptionSet();
Expand Down
25 changes: 25 additions & 0 deletions test/functional/sentinel.js
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,31 @@ describe('sentinel', function () {
});
});

it('should reject when sentinel is rejected', function (done) {
var sentinel = new MockServer(27379, function (argv) {
if (argv[0] === 'sentinel' && argv[1] === 'get-master-addr-by-name') {
return new Error('just rejected');
}
});

var redis = new Redis({
sentinels: [
{ host: '127.0.0.1', port: '27379' }
],
name: 'master',
sentinelRetryStrategy: null,
lazyConnect: true
});

redis.connect().then(function () {
throw new Error('Expect `connect` to be thrown');
}).catch(function (err) {
expect(err.message).to.eql('All sentinels are unreachable. Last error: just rejected');
redis.disconnect();
sentinel.disconnect(done);
});
});

it('should connect to the next sentinel if getting master failed', function (done) {
var sentinel = new MockServer(27379, function (argv) {
if (argv[0] === 'sentinel' && argv[1] === 'get-master-addr-by-name') {
Expand Down

0 comments on commit 3ca30d8

Please sign in to comment.