Skip to content

Commit 959b0ee

Browse files
author
Ruben Bridgewater
committed
Fix error codes for multi.exec and add more tests
1 parent 2293f7f commit 959b0ee

File tree

2 files changed

+13
-3
lines changed

2 files changed

+13
-3
lines changed

index.js

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1077,7 +1077,13 @@ Multi.prototype.execute_callback = function (err, replies) {
10771077
args = this.queue[i];
10781078

10791079
// If we asked for strings, even in detect_buffers mode, then return strings:
1080-
if (reply) {
1080+
if (reply instanceof Error) {
1081+
var match = reply.message.match(err_code);
1082+
// LUA script could return user errors that don't behave like all other errors!
1083+
if (match) {
1084+
reply.code = match[1];
1085+
}
1086+
} else if (reply) {
10811087
if (this._client.options.detect_buffers && this.wants_buffers[i] === false) {
10821088
replies[i - 1] = reply = reply_to_strings(reply);
10831089
}

test/commands/multi.spec.js

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -254,16 +254,19 @@ describe("The 'multi' method", function () {
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");
256256
assert.strictEqual(err.errors[0].command_used, 'SET');
257+
assert.strictEqual(err.errors[0].code, 'ERR');
257258
assert.strictEqual(err.errors[0].position, 1);
258259
assert(/^ERR/.test(err.errors[0].message), "Actuall error message should begin with ERR");
259260
return done();
260261
});
261262
});
262263

263264
it('reports multiple exceptions when they occur (while EXEC is running)', function (done) {
264-
client.multi().config("bar").debug("foo").exec(function (err, reply) {
265-
assert.strictEqual(reply.length, 2);
265+
client.multi().config("bar").debug("foo").eval("return {err='this is an error'}", 0).exec(function (err, reply) {
266+
assert.strictEqual(reply.length, 3);
266267
assert.equal(reply[0].code, 'ERR');
268+
assert.equal(reply[2].code, undefined);
269+
assert(/^this is an error/.test(reply[2].message));
267270
assert(/^ERR/.test(reply[0].message), "Error message should begin with ERR");
268271
assert(/^ERR/.test(reply[1].message), "Error message should begin with ERR");
269272
return done();
@@ -276,6 +279,7 @@ describe("The 'multi' method", function () {
276279
multi.set('foo', 'bar', helper.isString('OK'));
277280
multi.debug("foo").exec(function (err, reply) {
278281
assert.strictEqual(reply.length, 3);
282+
assert.strictEqual(reply[0].code, 'ERR');
279283
assert(/^ERR/.test(reply[0].message), "Error message should begin with ERR");
280284
assert(/^ERR/.test(reply[2].message), "Error message should begin with ERR");
281285
assert.strictEqual(reply[1], "OK");

0 commit comments

Comments
 (0)