Skip to content

Commit

Permalink
fix: select db in cluster mode causes unhandled errors (#1311)
Browse files Browse the repository at this point in the history
Closes #1310
  • Loading branch information
luin authored Mar 27, 2021
1 parent 7f81d0c commit da3ec92
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 1 deletion.
6 changes: 5 additions & 1 deletion lib/redis/event_handler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
18 changes: 18 additions & 0 deletions test/functional/connection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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();
});
}
});
});
});

0 comments on commit da3ec92

Please sign in to comment.