Skip to content

Commit 3ca30d8

Browse files
committed
fix(sentinel): improve the error message when connection to sentinel is rejected
Closes #280
1 parent 91998e3 commit 3ca30d8

File tree

3 files changed

+35
-3
lines changed

3 files changed

+35
-3
lines changed

lib/connectors/sentinel_connector.js

+9-2
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ SentinelConnector.prototype.connect = function (callback) {
3737
}
3838

3939
var _this = this;
40+
var lastError;
4041
connectToNext();
4142

4243
function connectToNext() {
@@ -50,7 +51,11 @@ SentinelConnector.prototype.connect = function (callback) {
5051
}
5152
if (typeof retryDelay !== 'number') {
5253
debug('All sentinels are unreachable and retry is disabled, emitting error...');
53-
return callback(new Error('All sentinels are unreachable.'));
54+
var error = 'All sentinels are unreachable.';
55+
if (lastError) {
56+
error += ' Last error: ' + lastError.message;
57+
}
58+
return callback(new Error(error));
5459
}
5560
debug('All sentinels are unreachable. Retrying from scratch after %d', retryDelay);
5661
setTimeout(connectToNext, retryDelay);
@@ -68,9 +73,11 @@ SentinelConnector.prototype.connect = function (callback) {
6873
callback(null, _this.stream);
6974
} else if (err) {
7075
debug('failed to connect to sentinel %s:%s because %s', endpoint.host, endpoint.port, err);
76+
lastError = err;
7177
connectToNext();
7278
} else {
73-
debug('connected to sentinel %s:%s successfully, but got a invalid reply: %s', endpoint.host, endpoint.port, resolved);
79+
debug('connected to sentinel %s:%s successfully, but got a invalid reply: %s',
80+
endpoint.host, endpoint.port, resolved);
7481
connectToNext();
7582
}
7683
});

lib/redis/parser.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -148,7 +148,7 @@ exports.returnReply = function (reply) {
148148
item = this.commandQueue.shift();
149149
if (!item) {
150150
return this.emit('error',
151-
new Error('Command queue state error. If you can reproduce this, please report it.' + reply.toString()));
151+
new Error('Command queue state error. If you can reproduce this, please report it. Last reply: ' + reply.toString()));
152152
}
153153
if (_.includes(Command.FLAGS.ENTER_SUBSCRIBER_MODE, item.command.name)) {
154154
this.condition.subscriber = new SubscriptionSet();

test/functional/sentinel.js

+25
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,31 @@ describe('sentinel', function () {
129129
});
130130
});
131131

132+
it('should reject when sentinel is rejected', function (done) {
133+
var sentinel = new MockServer(27379, function (argv) {
134+
if (argv[0] === 'sentinel' && argv[1] === 'get-master-addr-by-name') {
135+
return new Error('just rejected');
136+
}
137+
});
138+
139+
var redis = new Redis({
140+
sentinels: [
141+
{ host: '127.0.0.1', port: '27379' }
142+
],
143+
name: 'master',
144+
sentinelRetryStrategy: null,
145+
lazyConnect: true
146+
});
147+
148+
redis.connect().then(function () {
149+
throw new Error('Expect `connect` to be thrown');
150+
}).catch(function (err) {
151+
expect(err.message).to.eql('All sentinels are unreachable. Last error: just rejected');
152+
redis.disconnect();
153+
sentinel.disconnect(done);
154+
});
155+
});
156+
132157
it('should connect to the next sentinel if getting master failed', function (done) {
133158
var sentinel = new MockServer(27379, function (argv) {
134159
if (argv[0] === 'sentinel' && argv[1] === 'get-master-addr-by-name') {

0 commit comments

Comments
 (0)