diff --git a/lib/Redis.ts b/lib/Redis.ts index 2da6717ca..39441a017 100644 --- a/lib/Redis.ts +++ b/lib/Redis.ts @@ -1336,7 +1336,9 @@ class Redis this.condition?.select !== item.select && item.command.name !== "select" ) { - this.select(item.select); + this.select(item.select).catch((err) => + this.silentEmit("error", err) + ); } // TODO // @ts-expect-error diff --git a/test/unit/redis.ts b/test/unit/redis.ts index 17f639e45..796c82589 100644 --- a/test/unit/redis.ts +++ b/test/unit/redis.ts @@ -287,4 +287,22 @@ describe("Redis", () => { command.verify(); }); }); + + describe("#handleReconnection", () => { + it("emits an error when the db-restoring select fails on reconnectOnError", async () => { + const redis = new Redis({ lazyConnect: true, reconnectOnError: () => 2 }); + const selectError = new Error("ERR DB index is out of range"); + sinon.stub(redis, "disconnect"); + sinon.stub(redis, "sendCommand"); + sinon.stub(redis, "select").rejects(selectError); + const emitted = new Promise((resolve) => redis.once("error", resolve)); + + redis.handleReconnection(new Error("READONLY"), { + command: new Command("get", ["foo"]), + select: 2, + }); + + expect(await emitted).to.equal(selectError); + }); + }); });