Skip to content

Commit c60a3b6

Browse files
author
Ruben Bridgewater
committed
Rename .command_used to .command and add the used command to more errors
1 parent 1f121fa commit c60a3b6

File tree

5 files changed

+40
-29
lines changed

5 files changed

+40
-29
lines changed

index.js

Lines changed: 27 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -503,8 +503,11 @@ RedisClient.prototype.connection_gone = function (why) {
503503
var err_code = /^([A-Z]+)\s+(.+)$/;
504504
RedisClient.prototype.return_error = function (err) {
505505
var command_obj = this.command_queue.shift(), queue_len = this.command_queue.length;
506+
// send_command might have been used wrong => catch those cases too
506507
if (command_obj.command && command_obj.command.toUpperCase) {
507-
err.command_used = command_obj.command.toUpperCase();
508+
err.command = command_obj.command.toUpperCase();
509+
} else {
510+
err.command = command_obj.command;
508511
}
509512

510513
var match = err.message.match(err_code);
@@ -652,7 +655,9 @@ RedisClient.prototype.return_reply = function (reply) {
652655
});
653656
this.emit("monitor", timestamp, args);
654657
} else {
655-
this.emit("error", new Error("node_redis command queue state error. If you can reproduce this, please report it."));
658+
var err = new Error("node_redis command queue state error. If you can reproduce this, please report it.");
659+
err.command = command_obj.command.toUpperCase();
660+
this.emit("error", err);
656661
}
657662
};
658663

@@ -706,7 +711,7 @@ RedisClient.prototype.send_command = function (command, args, callback) {
706711
if (args[args.length - 1] === undefined || args[args.length - 1] === null) {
707712
command = command.toUpperCase();
708713
err = new Error('send_command: ' + command + ' value must not be undefined or null');
709-
err.command_used = command;
714+
err.command = command;
710715
if (callback) {
711716
return callback && callback(err);
712717
}
@@ -733,7 +738,7 @@ RedisClient.prototype.send_command = function (command, args, callback) {
733738
} else {
734739
err = new Error(command + ' can\'t be processed. The connection has already been closed.');
735740
}
736-
err.command_used = command;
741+
err.command = command;
737742
if (callback) {
738743
callback(err);
739744
} else {
@@ -754,7 +759,9 @@ RedisClient.prototype.send_command = function (command, args, callback) {
754759
} else if (command === "quit") {
755760
this.closing = true;
756761
} else if (this.pub_sub_mode === true) {
757-
this.emit("error", new Error("Connection in subscriber mode, only subscriber commands may be used"));
762+
err = new Error("Connection in subscriber mode, only subscriber commands may be used");
763+
err.command = command.toUpperCase();
764+
this.emit("error", err);
758765
return;
759766
}
760767
this.command_queue.push(command_obj);
@@ -935,7 +942,7 @@ RedisClient.prototype.select = RedisClient.prototype.SELECT = function (db, call
935942
RedisClient.prototype.auth = RedisClient.prototype.AUTH = function (pass, callback) {
936943
if (typeof pass !== 'string') {
937944
var err = new Error('The password has to be of type "string"');
938-
err.command_used = 'AUTH';
945+
err.command = 'AUTH';
939946
if (callback) {
940947
callback(err);
941948
} else {
@@ -1056,7 +1063,7 @@ Multi.prototype.exec = Multi.prototype.EXEC = function (callback) {
10561063
};
10571064

10581065
Multi.prototype.execute_callback = function (err, replies) {
1059-
var i, reply, args;
1066+
var i, args;
10601067

10611068
if (err) {
10621069
if (err.code !== 'CONNECTION_BROKEN') {
@@ -1072,32 +1079,32 @@ Multi.prototype.execute_callback = function (err, replies) {
10721079
}
10731080

10741081
if (replies) {
1075-
for (i = 1; i < this.queue.length; i += 1) {
1076-
reply = replies[i - 1];
1077-
args = this.queue[i];
1082+
for (i = 0; i < this.queue.length - 1; i += 1) {
1083+
args = this.queue[i + 1];
10781084

10791085
// If we asked for strings, even in detect_buffers mode, then return strings:
1080-
if (reply instanceof Error) {
1081-
var match = reply.message.match(err_code);
1086+
if (replies[i] instanceof Error) {
1087+
var match = replies[i].message.match(err_code);
10821088
// LUA script could return user errors that don't behave like all other errors!
10831089
if (match) {
1084-
reply.code = match[1];
1090+
replies[i].code = match[1];
10851091
}
1086-
} else if (reply) {
1087-
if (this._client.options.detect_buffers && this.wants_buffers[i] === false) {
1088-
replies[i - 1] = reply = reply_to_strings(reply);
1092+
replies[i].command = args[0].toUpperCase();
1093+
} else if (replies[i]) {
1094+
if (this._client.options.detect_buffers && this.wants_buffers[i + 1] === false) {
1095+
replies[i] = reply_to_strings(replies[i]);
10891096
}
10901097
if (args[0] === "hgetall") {
10911098
// TODO - confusing and error-prone that hgetall is special cased in two places
1092-
replies[i - 1] = reply = reply_to_object(reply);
1099+
replies[i] = reply_to_object(replies[i]);
10931100
}
10941101
}
10951102

10961103
if (typeof args[args.length - 1] === "function") {
1097-
if (reply instanceof Error) {
1098-
args[args.length - 1](reply);
1104+
if (replies[i] instanceof Error) {
1105+
args[args.length - 1](replies[i]);
10991106
} else {
1100-
args[args.length - 1](null, reply);
1107+
args[args.length - 1](null, replies[i]);
11011108
}
11021109
}
11031110
}

test/auth.spec.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ describe("client authentication", function () {
4141
client = redis.createClient.apply(redis.createClient, args);
4242

4343
client.once('error', function (err) {
44-
assert.strictEqual(err.command_used, 'AUTH');
44+
assert.strictEqual(err.command, 'AUTH');
4545
assert.ok(/ERR invalid password/.test(err.message));
4646
return done();
4747
});
@@ -55,7 +55,7 @@ describe("client authentication", function () {
5555
client = redis.createClient.apply(redis.createClient, args);
5656

5757
client.auth('', function (err, res) {
58-
assert.strictEqual(err.command_used, 'AUTH');
58+
assert.strictEqual(err.command, 'AUTH');
5959
assert.ok(/ERR invalid password/.test(err.message));
6060
done();
6161
});
@@ -130,7 +130,7 @@ describe("client authentication", function () {
130130
client = redis.createClient.apply(redis.createClient, args);
131131
client.auth(undefined, function(err, res) {
132132
assert.strictEqual(err.message, 'The password has to be of type "string"');
133-
assert.strictEqual(err.command_used, 'AUTH');
133+
assert.strictEqual(err.command, 'AUTH');
134134
assert.strictEqual(res, undefined);
135135
done();
136136
});
@@ -142,7 +142,7 @@ describe("client authentication", function () {
142142
client = redis.createClient.apply(redis.createClient, args);
143143
client.on('error', function (err) {
144144
assert.strictEqual(err.message, 'The password has to be of type "string"');
145-
assert.strictEqual(err.command_used, 'AUTH');
145+
assert.strictEqual(err.command, 'AUTH');
146146
done();
147147
});
148148
client.auth(234567);

test/commands/multi.spec.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -253,7 +253,7 @@ describe("The 'multi' method", function () {
253253
assert.equal(reply, undefined, "The reply should have been discarded");
254254
assert(err.message.match(/^EXECABORT/), "Error message should begin with EXECABORT");
255255
assert.equal(err.errors.length, 2, "err.errors should have 2 items");
256-
assert.strictEqual(err.errors[0].command_used, 'SET');
256+
assert.strictEqual(err.errors[0].command, 'SET');
257257
assert.strictEqual(err.errors[0].code, 'ERR');
258258
assert.strictEqual(err.errors[0].position, 1);
259259
assert(/^ERR/.test(err.errors[0].message), "Actuall error message should begin with ERR");
@@ -265,7 +265,9 @@ describe("The 'multi' method", function () {
265265
client.multi().config("bar").debug("foo").eval("return {err='this is an error'}", 0).exec(function (err, reply) {
266266
assert.strictEqual(reply.length, 3);
267267
assert.equal(reply[0].code, 'ERR');
268+
assert.equal(reply[0].command, 'CONFIG');
268269
assert.equal(reply[2].code, undefined);
270+
assert.equal(reply[2].command, 'EVAL');
269271
assert(/^this is an error/.test(reply[2].message));
270272
assert(/^ERR/.test(reply[0].message), "Error message should begin with ERR");
271273
assert(/^ERR/.test(reply[1].message), "Error message should begin with ERR");

test/commands/select.spec.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -94,7 +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');
97+
assert.strictEqual(err.command, 'SELECT');
9898
assert.equal(err.message, 'ERR invalid DB index');
9999
done();
100100
});

test/node_redis.spec.js

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -156,7 +156,9 @@ describe("The node_redis client", function () {
156156

157157
process.once('uncaughtException', function (err) {
158158
process.on('uncaughtException', mochaListener);
159-
assert(/ERR Protocol error/.test(err));
159+
assert(/ERR Protocol error/.test(err.message));
160+
assert.equal(err.command, true);
161+
assert.equal(err.code, 'ERR');
160162
done();
161163
});
162164

@@ -195,7 +197,7 @@ describe("The node_redis client", function () {
195197
setTimeout(function() {
196198
client.get("foo", function(err, res) {
197199
assert.strictEqual(err.message, 'GET can\'t be processed. The connection has already been closed.');
198-
assert.strictEqual(err.command_used, 'GET');
200+
assert.strictEqual(err.command, 'GET');
199201
assert.strictEqual(client.offline_queue.length, 0);
200202
done();
201203
});
@@ -209,7 +211,7 @@ describe("The node_redis client", function () {
209211
client.quit();
210212
client.on('error', function(err) {
211213
assert.strictEqual(err.message, 'SET can\'t be processed. The connection has already been closed.');
212-
assert.strictEqual(err.command_used, 'SET');
214+
assert.strictEqual(err.command, 'SET');
213215
assert.strictEqual(client.offline_queue.length, 0);
214216
done();
215217
});

0 commit comments

Comments
 (0)