Skip to content

Commit cb779a3

Browse files
committed
#2666 for v5
1 parent b99502b commit cb779a3

File tree

3 files changed

+43
-5
lines changed

3 files changed

+43
-5
lines changed

packages/client/lib/client/index.spec.ts

+18-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import testUtils, { GLOBAL, waitTillBeenCalled } from '../test-utils';
33
import RedisClient, { RedisClientType } from '.';
44
// import { RedisClientMultiCommandType } from './multi-command';
55
// import { RedisCommandRawReply, RedisModules, RedisFunctions, RedisScripts } from '../commands';
6-
import { AbortError, ClientClosedError, ClientOfflineError, ConnectionTimeoutError, DisconnectsClientError, SocketClosedUnexpectedlyError, WatchError } from '../errors';
6+
import { AbortError, ClientClosedError, ClientOfflineError, ConnectionTimeoutError, DisconnectsClientError, ErrorReply, MultiErrorReply, SocketClosedUnexpectedlyError, WatchError } from '../errors';
77
import { defineScript } from '../lua-script';
88
import { spy } from 'sinon';
99
import { once } from 'node:events';
@@ -282,6 +282,23 @@ describe('Client', () => {
282282
// ...GLOBAL.SERVERS.OPEN,
283283
// minimumDockerVersion: [6, 2] // CLIENT INFO
284284
// });
285+
286+
287+
testUtils.testWithClient('should handle error replies (#2665)', async client => {
288+
await assert.rejects(
289+
client.multi()
290+
.set('key', 'value')
291+
.hGetAll('key')
292+
.exec(),
293+
err => {
294+
assert.ok(err instanceof MultiErrorReply);
295+
assert.equal(err.replies.length, 2);
296+
assert.deepEqual(err.errorIndexes, [1]);
297+
assert.ok(err.replies[1] instanceof ErrorReply);
298+
return true;
299+
}
300+
);
301+
}, GLOBAL.SERVERS.OPEN);
285302
});
286303

287304
testUtils.testWithClient('scripts', async client => {

packages/client/lib/errors.ts

+11
Original file line numberDiff line numberDiff line change
@@ -69,3 +69,14 @@ export class SimpleError extends ErrorReply {}
6969
export class BlobError extends ErrorReply {}
7070

7171
export class TimeoutError extends Error {}
72+
73+
export class MultiErrorReply extends ErrorReply {
74+
replies: Array<ErrorReply>;
75+
errorIndexes: Array<number>;
76+
77+
constructor(replies: Array<ErrorReply>, errorIndexes: Array<number>) {
78+
super(`${errorIndexes.length} commands failed, see .replies and .errorIndexes for more information`);
79+
this.replies = replies;
80+
this.errorIndexes = errorIndexes;
81+
}
82+
}

packages/client/lib/multi-command.ts

+14-4
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { CommandArguments, RedisScript, ReplyUnion, TransformReply } from './RESP/types';
2+
import { ErrorReply, MultiErrorReply } from './errors';
23

34
export type MULTI_REPLY = {
45
GENERIC: 'generic';
@@ -46,9 +47,18 @@ export default class RedisMultiCommand {
4647
}
4748

4849
transformReplies(rawReplies: Array<unknown>): Array<unknown> {
49-
return rawReplies.map((reply, i) => {
50-
const { transformReply, args } = this.queue[i];
51-
return transformReply ? transformReply(reply, args.preserve) : reply;
52-
});
50+
const errorIndexes: Array<number> = [],
51+
replies = rawReplies.map((reply, i) => {
52+
if (reply instanceof ErrorReply) {
53+
errorIndexes.push(i);
54+
return reply;
55+
}
56+
57+
const { transformReply, args } = this.queue[i];
58+
return transformReply ? transformReply(reply, args.preserve) : reply;
59+
});
60+
61+
if (errorIndexes.length) throw new MultiErrorReply(replies, errorIndexes);
62+
return replies;
5363
}
5464
}

0 commit comments

Comments
 (0)