#1648·ioredis

Suggestion for enhancement on doc and type about ETIMEDOUT error

Author: lomutoCreated Sep 8, 2022Updated Sep 4, 2026
Labelsstale

I am currently using ioredis 5.0.5 version Node.js 14

I would like to suggest some document and type enhancement with ETIMEDOUT error

The issue I have experienced was passing a wrong host address or port number to Redis constructor.

const HOST_ADDRESS = "https://wrong.host.address";
const PORT = 6379;
const redisClient = new Redis({ host: HOST_ADDRESS, port: PORT });

If the given address is invalid and ioredis failed to get respond, ioredis client object catches the ETIMEDOUT and print the stacktrace, try to reconnect untill it success while blocking the program

// this stack trace keep prints and running script can not move foward
[ioredis] Unhandled error event: Error: connect ETIMEDOUT
    at Socket.<anonymous> (.../node_modules/ioredis/built/Redis.js:168:41)
    at Object.onceWrapper (events.js:481:28)
    at Socket.emit (events.js:375:28)
    at Socket._onTimeout (net.js:484:8)
    at listOnTimeout (internal/timers.js:557:17)
    at processTimers (internal/timers.js:500:7)
[ioredis] Unhandled error event: Error: connect ETIMEDOUT
    at Socket.<anonymous> (.../node_modules/ioredis/built/Redis.js:168:41)
    at Object.onceWrapper (events.js:481:28)
    at Socket.emit (events.js:375:28)
    at Socket._onTimeout (net.js:484:8)
    at listOnTimeout (internal/timers.js:557:17)
    at processTimers (internal/timers.js:500:7)
[ioredis] Unhandled error event: Error: connect ETIMEDOUT
...

I think the reason why ioredis keep attempting to connect server till its success is quite reasonable, such as temporary network issue. But I strongly believe that user must take full responsibility on any kind of unexpected events, even if it is a temporary issue. And also letting program crash is better than leaving it self as idle or become zombie.

So instead of changing the current ioredis' self error catch logic, adding some guidance of how to catch error in customized way on documentation and typing in redisClient's on method arguement might be helpful

This is how I solved current issue

const redisClient = new Redis({ host: HOST_ADDRESS, port: PORT });
redisClient.on("error", (err) => { throw new Error(`Error from redisClient: ${err}`); });

Since ioredis client object extends EventEmitter I've found out that adding a callback with single parameter on error event in on method, enables user to catch ETIMEDOUT error in a customized way, not attempting to re-connect

But current ioredis on method typing on eventName is string | symbol, I would like to enhance type by adding error event so that user can inference that it could choose its own way to handle error events

// current type
on(eventName: string | symbol, listener: (...args: any[]) => void): this;

// to-be
on(eventName: string | symbol | "error", listener: (...args: any[]) => void): this;

Thanks for reading this suggestion! Looking forward for any thoughts about this idea :)