Skip to content

Commit a5acc61

Browse files
committed
Merge branch 'feat/auth-error'
2 parents 8f3bb6a + 56cb047 commit a5acc61

File tree

6 files changed

+69
-22
lines changed

6 files changed

+69
-22
lines changed

README.md

-1
Original file line numberDiff line numberDiff line change
@@ -563,7 +563,6 @@ Besides the above connection events, there are several other custom events:
563563

564564
Event | Description
565565
:------------- | :-------------
566-
authError | emits when the password specified in the options is wrong or the server doesn't require a password.
567566
select | emits when the database changed. The argument is the new db number.
568567

569568
## Offline Queue

lib/cluster/index.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -337,7 +337,7 @@ Cluster.prototype.refreshSlotsCache = function (callback) {
337337
};
338338

339339
/**
340-
* Flush offline queue and command queue with error.
340+
* Flush offline queue with error.
341341
*
342342
* @param {Error} error - The error object to send to the commands
343343
* @private

lib/redis.js

+8-3
Original file line numberDiff line numberDiff line change
@@ -377,9 +377,14 @@ Redis.prototype.flushQueue = function (error, options) {
377377
}
378378

379379
if (options.commandQueue) {
380-
while (this.commandQueue.length > 0) {
381-
item = this.commandQueue.shift();
382-
item.command.reject(error);
380+
if (this.commandQueue.length > 0) {
381+
if (this.stream) {
382+
this.stream.removeAllListeners('data');
383+
}
384+
while (this.commandQueue.length > 0) {
385+
item = this.commandQueue.shift();
386+
item.command.reject(error);
387+
}
383388
}
384389
}
385390
};

lib/redis/event_handler.js

+13-4
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,18 @@ exports.connectHandler = function (self) {
1111
self.resetCommandQueue();
1212

1313
// AUTH command should be processed before any other commands
14+
var flushed = false;
1415
if (self.condition.auth) {
1516
self.auth(self.condition.auth, function (err) {
1617
if (err) {
17-
self.emit('authError', err);
18+
if (err.message.indexOf('no password is set') === -1) {
19+
flushed = true;
20+
self.flushQueue(err);
21+
self.silentEmit('error', err);
22+
self.disconnect(true);
23+
} else {
24+
console.warn('[WARN] Redis server does not require a password, but a password was supplied.');
25+
}
1826
}
1927
});
2028
}
@@ -32,9 +40,10 @@ exports.connectHandler = function (self) {
3240
if (self.options.enableReadyCheck) {
3341
self._readyCheck(function (err, info) {
3442
if (err) {
35-
self.flushQueue(new Error('Ready check failed: ' + err.message));
36-
if (!self.condition.auth && err.message.split(' ')[0] === 'NOAUTH') {
37-
self.emit('authError', err);
43+
if (!flushed) {
44+
self.flushQueue(new Error('Ready check failed: ' + err.message));
45+
self.silentEmit('error', err);
46+
self.disconnect(true);
3847
}
3948
} else {
4049
self.serverInfo = info;

test/functional/auth.js

+31-13
Original file line numberDiff line numberDiff line change
@@ -41,44 +41,62 @@ describe('auth', function () {
4141
});
4242
});
4343

44-
it('should emit "authError" when the server doesn\'t need auth', function (done) {
44+
it('should not emit "error" when the server doesn\'t need auth', function (done) {
4545
var server = new MockServer(17379, function (argv) {
4646
if (argv[0] === 'auth' && argv[1] === 'pass') {
4747
return new Error('ERR Client sent AUTH, but no password is set');
4848
}
4949
});
50+
var errorEmited = false;
5051
var redis = new Redis({ port: 17379, password: 'pass' });
51-
redis.on('authError', function (error) {
52-
expect(error).to.have.property('message', 'ERR Client sent AUTH, but no password is set');
53-
redis.disconnect();
54-
server.disconnect();
55-
done();
52+
redis.on('error', function () {
53+
errorEmited = true;
54+
});
55+
stub(console, 'warn', function (warn) {
56+
expect(warn).to.match(/but a password was supplied/);
57+
console.warn.restore();
58+
setTimeout(function () {
59+
expect(errorEmited).to.eql(false);
60+
redis.disconnect();
61+
server.disconnect();
62+
done();
63+
}, 0);
5664
});
5765
});
5866

59-
it('should emit "authError" when the password is wrong', function (done) {
67+
it('should emit "error" when the password is wrong', function (done) {
6068
var server = new MockServer(17379, function (argv) {
6169
if (argv[0] === 'auth' && argv[1] === 'pass') {
6270
return new Error('ERR invalid password');
6371
}
6472
});
6573
var redis = new Redis({ port: 17379, password: 'pass' });
66-
redis.on('authError', function (error) {
74+
var pending = 2;
75+
function check() {
76+
if (!--pending) {
77+
redis.disconnect();
78+
server.disconnect();
79+
done();
80+
}
81+
}
82+
redis.on('error', function (error) {
6783
expect(error).to.have.property('message', 'ERR invalid password');
68-
redis.disconnect();
69-
server.disconnect();
70-
done();
84+
check();
85+
});
86+
redis.get('foo', function (err, res) {
87+
expect(err.message).to.eql('ERR invalid password');
88+
check();
7189
});
7290
});
7391

74-
it('should emit "authError" when password is not provided', function (done) {
92+
it('should emit "error" when password is not provided', function (done) {
7593
var server = new MockServer(17379, function (argv) {
7694
if (argv[0] === 'info') {
7795
return new Error('NOAUTH Authentication required.');
7896
}
7997
});
8098
var redis = new Redis({ port: 17379 });
81-
redis.on('authError', function (error) {
99+
redis.on('error', function (error) {
82100
expect(error).to.have.property('message', 'NOAUTH Authentication required.');
83101
redis.disconnect();
84102
server.disconnect();

test/functional/ready_check.js

+16
Original file line numberDiff line numberDiff line change
@@ -16,4 +16,20 @@ describe('ready_check', function () {
1616
});
1717
redis.connect();
1818
});
19+
20+
it('should reconnect when info return a error', function (done) {
21+
var redis = new Redis({
22+
lazyConnect: true,
23+
retryStrategy: function () {
24+
done();
25+
return;
26+
}
27+
});
28+
29+
stub(redis, 'info', function (callback) {
30+
callback(new Error('info error'));
31+
});
32+
33+
redis.connect();
34+
});
1935
});

0 commit comments

Comments
 (0)