Skip to content

Commit e24f056

Browse files
committed
Merge pull request #841 from fintura/command
Add .command_used to errors returned by the parser. Fix authentication failure being emitted instead of returned by a callback if present. Fixes #427
2 parents e4bd9bf + ebbb014 commit e24f056

File tree

3 files changed

+37
-9
lines changed

3 files changed

+37
-9
lines changed

index.js

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -206,20 +206,20 @@ RedisClient.prototype.do_auth = function () {
206206
debug("Warning: Redis server does not require a password, but a password was supplied.");
207207
err = null;
208208
res = "OK";
209+
} else if (self.auth_callback) {
210+
self.auth_callback(err);
211+
self.auth_callback = null;
209212
} else {
210-
return self.emit("error", new Error("Auth error: " + err.message));
213+
self.emit("error", err);
214+
return;
211215
}
212216
}
213217

214218
res = res.toString();
215-
if (res !== "OK") {
216-
return self.emit("error", new Error("Auth failed: " + res));
217-
}
218-
219219
debug("Auth succeeded " + self.address + " id " + self.connection_id);
220220

221221
if (self.auth_callback) {
222-
self.auth_callback(err, res);
222+
self.auth_callback(null, res);
223223
self.auth_callback = null;
224224
}
225225

@@ -509,6 +509,7 @@ RedisClient.prototype.on_data = function (data) {
509509

510510
RedisClient.prototype.return_error = function (err) {
511511
var command_obj = this.command_queue.shift(), queue_len = this.command_queue.length;
512+
err.command_used = command_obj.command.toUpperCase();
512513

513514
if (this.pub_sub_mode === false && queue_len === 0) {
514515
this.command_queue = new Queue();

test/auth.spec.js

Lines changed: 29 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -34,19 +34,32 @@ describe("client authentication", function () {
3434
});
3535
});
3636

37-
it("raises error when auth is bad", function (done) {
37+
it("emits error when auth is bad without callback", function (done) {
3838
if (helper.redisProcess().spawnFailed()) this.skip();
3939

4040
client = redis.createClient.apply(redis.createClient, args);
4141

42-
client.once('error', function (error) {
43-
assert.ok(/ERR invalid password/.test(error));
42+
client.once('error', function (err) {
43+
assert.strictEqual(err.command_used, 'AUTH');
44+
assert.ok(/ERR invalid password/.test(err.message));
4445
return done();
4546
});
4647

4748
client.auth(auth + 'bad');
4849
});
4950

51+
it("returns an error when auth is bad with a callback", function (done) {
52+
if (helper.redisProcess().spawnFailed()) this.skip();
53+
54+
client = redis.createClient.apply(redis.createClient, args);
55+
56+
client.auth(auth + 'bad', function (err, res) {
57+
assert.strictEqual(err.command_used, 'AUTH');
58+
assert.ok(/ERR invalid password/.test(err.message));
59+
done();
60+
});
61+
});
62+
5063
if (ip === 'IPv4') {
5164
it('allows auth to be provided as part of redis url', function (done) {
5265
if (helper.redisProcess().spawnFailed()) this.skip();
@@ -70,6 +83,19 @@ describe("client authentication", function () {
7083
});
7184
});
7285

86+
it('allows auth and no_ready_check to be provided as config option for client', function (done) {
87+
if (helper.redisProcess().spawnFailed()) this.skip();
88+
89+
var args = config.configureClient(parser, ip, {
90+
auth_pass: auth,
91+
no_ready_check: true
92+
});
93+
client = redis.createClient.apply(redis.createClient, args);
94+
client.on("ready", function () {
95+
done();
96+
});
97+
});
98+
7399
it('allows auth to be provided post-hoc with auth method', function (done) {
74100
if (helper.redisProcess().spawnFailed()) this.skip();
75101

test/commands/select.spec.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,7 @@ describe("The 'select' method", function () {
9494
assert.strictEqual(client.selected_db, null, "default db should be null");
9595

9696
client.on('error', function (err) {
97+
assert.strictEqual(err.command_used, 'SELECT');
9798
assert.equal(err.message, 'ERR invalid DB index');
9899
done();
99100
});

0 commit comments

Comments
 (0)