1
0
mirror of https://github.com/redis/node-redis.git synced 2025-08-04 15:02:09 +03:00

Rename .command_used to .command and add the used command to more errors

This commit is contained in:
Ruben Bridgewater
2015-09-20 18:56:21 +02:00
parent 1f121fa6e2
commit c60a3b65fe
5 changed files with 40 additions and 29 deletions

View File

@@ -503,8 +503,11 @@ RedisClient.prototype.connection_gone = function (why) {
var err_code = /^([A-Z]+)\s+(.+)$/; var err_code = /^([A-Z]+)\s+(.+)$/;
RedisClient.prototype.return_error = function (err) { RedisClient.prototype.return_error = function (err) {
var command_obj = this.command_queue.shift(), queue_len = this.command_queue.length; var command_obj = this.command_queue.shift(), queue_len = this.command_queue.length;
// send_command might have been used wrong => catch those cases too
if (command_obj.command && command_obj.command.toUpperCase) { if (command_obj.command && command_obj.command.toUpperCase) {
err.command_used = command_obj.command.toUpperCase(); err.command = command_obj.command.toUpperCase();
} else {
err.command = command_obj.command;
} }
var match = err.message.match(err_code); var match = err.message.match(err_code);
@@ -652,7 +655,9 @@ RedisClient.prototype.return_reply = function (reply) {
}); });
this.emit("monitor", timestamp, args); this.emit("monitor", timestamp, args);
} else { } else {
this.emit("error", new Error("node_redis command queue state error. If you can reproduce this, please report it.")); var err = new Error("node_redis command queue state error. If you can reproduce this, please report it.");
err.command = command_obj.command.toUpperCase();
this.emit("error", err);
} }
}; };
@@ -706,7 +711,7 @@ RedisClient.prototype.send_command = function (command, args, callback) {
if (args[args.length - 1] === undefined || args[args.length - 1] === null) { if (args[args.length - 1] === undefined || args[args.length - 1] === null) {
command = command.toUpperCase(); command = command.toUpperCase();
err = new Error('send_command: ' + command + ' value must not be undefined or null'); err = new Error('send_command: ' + command + ' value must not be undefined or null');
err.command_used = command; err.command = command;
if (callback) { if (callback) {
return callback && callback(err); return callback && callback(err);
} }
@@ -733,7 +738,7 @@ RedisClient.prototype.send_command = function (command, args, callback) {
} else { } else {
err = new Error(command + ' can\'t be processed. The connection has already been closed.'); err = new Error(command + ' can\'t be processed. The connection has already been closed.');
} }
err.command_used = command; err.command = command;
if (callback) { if (callback) {
callback(err); callback(err);
} else { } else {
@@ -754,7 +759,9 @@ RedisClient.prototype.send_command = function (command, args, callback) {
} else if (command === "quit") { } else if (command === "quit") {
this.closing = true; this.closing = true;
} else if (this.pub_sub_mode === true) { } else if (this.pub_sub_mode === true) {
this.emit("error", new Error("Connection in subscriber mode, only subscriber commands may be used")); err = new Error("Connection in subscriber mode, only subscriber commands may be used");
err.command = command.toUpperCase();
this.emit("error", err);
return; return;
} }
this.command_queue.push(command_obj); this.command_queue.push(command_obj);
@@ -935,7 +942,7 @@ RedisClient.prototype.select = RedisClient.prototype.SELECT = function (db, call
RedisClient.prototype.auth = RedisClient.prototype.AUTH = function (pass, callback) { RedisClient.prototype.auth = RedisClient.prototype.AUTH = function (pass, callback) {
if (typeof pass !== 'string') { if (typeof pass !== 'string') {
var err = new Error('The password has to be of type "string"'); var err = new Error('The password has to be of type "string"');
err.command_used = 'AUTH'; err.command = 'AUTH';
if (callback) { if (callback) {
callback(err); callback(err);
} else { } else {
@@ -1056,7 +1063,7 @@ Multi.prototype.exec = Multi.prototype.EXEC = function (callback) {
}; };
Multi.prototype.execute_callback = function (err, replies) { Multi.prototype.execute_callback = function (err, replies) {
var i, reply, args; var i, args;
if (err) { if (err) {
if (err.code !== 'CONNECTION_BROKEN') { if (err.code !== 'CONNECTION_BROKEN') {
@@ -1072,32 +1079,32 @@ Multi.prototype.execute_callback = function (err, replies) {
} }
if (replies) { if (replies) {
for (i = 1; i < this.queue.length; i += 1) { for (i = 0; i < this.queue.length - 1; i += 1) {
reply = replies[i - 1]; args = this.queue[i + 1];
args = this.queue[i];
// If we asked for strings, even in detect_buffers mode, then return strings: // If we asked for strings, even in detect_buffers mode, then return strings:
if (reply instanceof Error) { if (replies[i] instanceof Error) {
var match = reply.message.match(err_code); var match = replies[i].message.match(err_code);
// LUA script could return user errors that don't behave like all other errors! // LUA script could return user errors that don't behave like all other errors!
if (match) { if (match) {
reply.code = match[1]; replies[i].code = match[1];
} }
} else if (reply) { replies[i].command = args[0].toUpperCase();
if (this._client.options.detect_buffers && this.wants_buffers[i] === false) { } else if (replies[i]) {
replies[i - 1] = reply = reply_to_strings(reply); if (this._client.options.detect_buffers && this.wants_buffers[i + 1] === false) {
replies[i] = reply_to_strings(replies[i]);
} }
if (args[0] === "hgetall") { if (args[0] === "hgetall") {
// TODO - confusing and error-prone that hgetall is special cased in two places // TODO - confusing and error-prone that hgetall is special cased in two places
replies[i - 1] = reply = reply_to_object(reply); replies[i] = reply_to_object(replies[i]);
} }
} }
if (typeof args[args.length - 1] === "function") { if (typeof args[args.length - 1] === "function") {
if (reply instanceof Error) { if (replies[i] instanceof Error) {
args[args.length - 1](reply); args[args.length - 1](replies[i]);
} else { } else {
args[args.length - 1](null, reply); args[args.length - 1](null, replies[i]);
} }
} }
} }

View File

@@ -41,7 +41,7 @@ describe("client authentication", function () {
client = redis.createClient.apply(redis.createClient, args); client = redis.createClient.apply(redis.createClient, args);
client.once('error', function (err) { client.once('error', function (err) {
assert.strictEqual(err.command_used, 'AUTH'); assert.strictEqual(err.command, 'AUTH');
assert.ok(/ERR invalid password/.test(err.message)); assert.ok(/ERR invalid password/.test(err.message));
return done(); return done();
}); });
@@ -55,7 +55,7 @@ describe("client authentication", function () {
client = redis.createClient.apply(redis.createClient, args); client = redis.createClient.apply(redis.createClient, args);
client.auth('', function (err, res) { client.auth('', function (err, res) {
assert.strictEqual(err.command_used, 'AUTH'); assert.strictEqual(err.command, 'AUTH');
assert.ok(/ERR invalid password/.test(err.message)); assert.ok(/ERR invalid password/.test(err.message));
done(); done();
}); });
@@ -130,7 +130,7 @@ describe("client authentication", function () {
client = redis.createClient.apply(redis.createClient, args); client = redis.createClient.apply(redis.createClient, args);
client.auth(undefined, function(err, res) { client.auth(undefined, function(err, res) {
assert.strictEqual(err.message, 'The password has to be of type "string"'); assert.strictEqual(err.message, 'The password has to be of type "string"');
assert.strictEqual(err.command_used, 'AUTH'); assert.strictEqual(err.command, 'AUTH');
assert.strictEqual(res, undefined); assert.strictEqual(res, undefined);
done(); done();
}); });
@@ -142,7 +142,7 @@ describe("client authentication", function () {
client = redis.createClient.apply(redis.createClient, args); client = redis.createClient.apply(redis.createClient, args);
client.on('error', function (err) { client.on('error', function (err) {
assert.strictEqual(err.message, 'The password has to be of type "string"'); assert.strictEqual(err.message, 'The password has to be of type "string"');
assert.strictEqual(err.command_used, 'AUTH'); assert.strictEqual(err.command, 'AUTH');
done(); done();
}); });
client.auth(234567); client.auth(234567);

View File

@@ -253,7 +253,7 @@ describe("The 'multi' method", function () {
assert.equal(reply, undefined, "The reply should have been discarded"); assert.equal(reply, undefined, "The reply should have been discarded");
assert(err.message.match(/^EXECABORT/), "Error message should begin with EXECABORT"); assert(err.message.match(/^EXECABORT/), "Error message should begin with EXECABORT");
assert.equal(err.errors.length, 2, "err.errors should have 2 items"); assert.equal(err.errors.length, 2, "err.errors should have 2 items");
assert.strictEqual(err.errors[0].command_used, 'SET'); assert.strictEqual(err.errors[0].command, 'SET');
assert.strictEqual(err.errors[0].code, 'ERR'); assert.strictEqual(err.errors[0].code, 'ERR');
assert.strictEqual(err.errors[0].position, 1); assert.strictEqual(err.errors[0].position, 1);
assert(/^ERR/.test(err.errors[0].message), "Actuall error message should begin with ERR"); assert(/^ERR/.test(err.errors[0].message), "Actuall error message should begin with ERR");
@@ -265,7 +265,9 @@ describe("The 'multi' method", function () {
client.multi().config("bar").debug("foo").eval("return {err='this is an error'}", 0).exec(function (err, reply) { client.multi().config("bar").debug("foo").eval("return {err='this is an error'}", 0).exec(function (err, reply) {
assert.strictEqual(reply.length, 3); assert.strictEqual(reply.length, 3);
assert.equal(reply[0].code, 'ERR'); assert.equal(reply[0].code, 'ERR');
assert.equal(reply[0].command, 'CONFIG');
assert.equal(reply[2].code, undefined); assert.equal(reply[2].code, undefined);
assert.equal(reply[2].command, 'EVAL');
assert(/^this is an error/.test(reply[2].message)); assert(/^this is an error/.test(reply[2].message));
assert(/^ERR/.test(reply[0].message), "Error message should begin with ERR"); assert(/^ERR/.test(reply[0].message), "Error message should begin with ERR");
assert(/^ERR/.test(reply[1].message), "Error message should begin with ERR"); assert(/^ERR/.test(reply[1].message), "Error message should begin with ERR");

View File

@@ -94,7 +94,7 @@ describe("The 'select' method", function () {
assert.strictEqual(client.selected_db, null, "default db should be null"); assert.strictEqual(client.selected_db, null, "default db should be null");
client.on('error', function (err) { client.on('error', function (err) {
assert.strictEqual(err.command_used, 'SELECT'); assert.strictEqual(err.command, 'SELECT');
assert.equal(err.message, 'ERR invalid DB index'); assert.equal(err.message, 'ERR invalid DB index');
done(); done();
}); });

View File

@@ -156,7 +156,9 @@ describe("The node_redis client", function () {
process.once('uncaughtException', function (err) { process.once('uncaughtException', function (err) {
process.on('uncaughtException', mochaListener); process.on('uncaughtException', mochaListener);
assert(/ERR Protocol error/.test(err)); assert(/ERR Protocol error/.test(err.message));
assert.equal(err.command, true);
assert.equal(err.code, 'ERR');
done(); done();
}); });
@@ -195,7 +197,7 @@ describe("The node_redis client", function () {
setTimeout(function() { setTimeout(function() {
client.get("foo", function(err, res) { client.get("foo", function(err, res) {
assert.strictEqual(err.message, 'GET can\'t be processed. The connection has already been closed.'); assert.strictEqual(err.message, 'GET can\'t be processed. The connection has already been closed.');
assert.strictEqual(err.command_used, 'GET'); assert.strictEqual(err.command, 'GET');
assert.strictEqual(client.offline_queue.length, 0); assert.strictEqual(client.offline_queue.length, 0);
done(); done();
}); });
@@ -209,7 +211,7 @@ describe("The node_redis client", function () {
client.quit(); client.quit();
client.on('error', function(err) { client.on('error', function(err) {
assert.strictEqual(err.message, 'SET can\'t be processed. The connection has already been closed.'); assert.strictEqual(err.message, 'SET can\'t be processed. The connection has already been closed.');
assert.strictEqual(err.command_used, 'SET'); assert.strictEqual(err.command, 'SET');
assert.strictEqual(client.offline_queue.length, 0); assert.strictEqual(client.offline_queue.length, 0);
done(); done();
}); });