diff --git a/lib/redis/event_handler.ts b/lib/redis/event_handler.ts index 9932d1eb..618d8aa2 100644 --- a/lib/redis/event_handler.ts +++ b/lib/redis/event_handler.ts @@ -54,7 +54,11 @@ export function connectHandler(self) { } if (self.condition.select) { - self.select(self.condition.select); + self.select(self.condition.select).catch((err) => { + // If the node is in cluster mode, select is disallowed. + // In this case, reconnect won't help. + self.silentEmit("error", err); + }); } if (!self.options.enableReadyCheck) { diff --git a/test/functional/connection.ts b/test/functional/connection.ts index a527054c..ef4a3447 100644 --- a/test/functional/connection.ts +++ b/test/functional/connection.ts @@ -531,4 +531,22 @@ describe("disconnection", function () { done(); }); }); + + it("emits an error if select is not allowed", function (done) { + const errMessage = "select is not allowed"; + const node = new MockServer(30001, function (argv) { + if (argv[0] === "select") { + return new Error(errMessage); + } + }); + const redis = new Redis({ port: 30001, db: 2 }); + redis.on("error", (err) => { + if (err.message === errMessage) { + redis.disconnect(); + node.disconnect(() => { + done(); + }); + } + }); + }); });