#How to gracefully handle ECONNREFUSED errors?

2 messages · Page 1 of 1 (latest)

blissful oyster
#

If the given url is not proper or not valid or not able to connect, just let me move, don't hold me there, how can achive this?

I don't want the error to be occuring recursively causing halt of all the other process, ideally I guess I need to stop the event handler, How do i do that?
return is not working.

import dotenv from 'dotenv';
import { createClient } from 'redis';
dotenv.config();

let redis;
if (process.env.REDIS_URL) {
  try {
    redis = await createClient({
      url: process.env.REDIS_URL,
      disableOfflineQueue: true,
      commandsQueueMaxLength: 1,
    })
      .on('error', (err) => {
        console.log('Redis Client Error', err);
        return null;
      })
      .connect();
    console.log('Redis Connected');
  } catch (error) {
    console.error('Error connecting to Redis:', error.message);
    redis = null;
  }
} else {
  console.log('Redis not configured, cache disabled.');
}

export { redis };

proven shell
#

i think the error handler is non-blocking already?

Also not sure but what you are asking is not possible imho. maybe a redis expert can give more detail on it.

But you can slow down the error logs and throw an error after a number of retries with reconnectStrategy

https://redis.io/docs/latest/develop/connect/clients/nodejs/