From 959b0ee093952626be68d1539f9905c2db4c4b47 Mon Sep 17 00:00:00 2001 From: Ruben Bridgewater Date: Sat, 19 Sep 2015 18:15:23 +0200 Subject: [PATCH] Fix error codes for multi.exec and add more tests --- index.js | 8 +++++++- test/commands/multi.spec.js | 8 ++++++-- 2 files changed, 13 insertions(+), 3 deletions(-) diff --git a/index.js b/index.js index 9e93ef3c5e..98b2267c15 100644 --- a/index.js +++ b/index.js @@ -1077,7 +1077,13 @@ Multi.prototype.execute_callback = function (err, replies) { args = this.queue[i]; // If we asked for strings, even in detect_buffers mode, then return strings: - if (reply) { + if (reply instanceof Error) { + var match = reply.message.match(err_code); + // LUA script could return user errors that don't behave like all other errors! + if (match) { + reply.code = match[1]; + } + } else if (reply) { if (this._client.options.detect_buffers && this.wants_buffers[i] === false) { replies[i - 1] = reply = reply_to_strings(reply); } diff --git a/test/commands/multi.spec.js b/test/commands/multi.spec.js index e6c72d01dc..840eb9687c 100644 --- a/test/commands/multi.spec.js +++ b/test/commands/multi.spec.js @@ -254,6 +254,7 @@ describe("The 'multi' method", function () { assert(err.message.match(/^EXECABORT/), "Error message should begin with EXECABORT"); 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].code, 'ERR'); assert.strictEqual(err.errors[0].position, 1); assert(/^ERR/.test(err.errors[0].message), "Actuall error message should begin with ERR"); return done(); @@ -261,9 +262,11 @@ describe("The 'multi' method", function () { }); it('reports multiple exceptions when they occur (while EXEC is running)', function (done) { - client.multi().config("bar").debug("foo").exec(function (err, reply) { - assert.strictEqual(reply.length, 2); + client.multi().config("bar").debug("foo").eval("return {err='this is an error'}", 0).exec(function (err, reply) { + assert.strictEqual(reply.length, 3); assert.equal(reply[0].code, 'ERR'); + assert.equal(reply[2].code, undefined); + 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[1].message), "Error message should begin with ERR"); return done(); @@ -276,6 +279,7 @@ describe("The 'multi' method", function () { multi.set('foo', 'bar', helper.isString('OK')); multi.debug("foo").exec(function (err, reply) { assert.strictEqual(reply.length, 3); + assert.strictEqual(reply[0].code, 'ERR'); assert(/^ERR/.test(reply[0].message), "Error message should begin with ERR"); assert(/^ERR/.test(reply[2].message), "Error message should begin with ERR"); assert.strictEqual(reply[1], "OK");