Node-redis: Cannot handle connection error using Redis with Promises

Created on 16 Jul 2016  路  1Comment  路  Source: NodeRedis/node-redis

  • Version: node_redis 2.6.2 | redis 3.0.7
  • Platform: Node.js 5.5.0 on OS X El Capitan
  • Description:

Hi!

I cannot achieve to catch properly a Redis connection error using Promises.
Here is my code:

'use strict';
const P = require('bluebird');
const Redis = require('redis');
P.promisifyAll(Redis.RedisClient.prototype);

function RenderingCache(renderingId) {
  const key = `/api/renderings/${renderingId}`;

  this.get = function () {
    const redis = Redis.createClient();
    return redis
      .getAsync(key)
      .then((value) => {
        redis.quit();
        return value;
      });
  };

  /* ... */
}

module.exports = RenderingCache;

Everything works fine if the Redis server is running.
But if I stop the Redis server, I want my code to continue to work without the cache.

To handle it, I tried several things.
This code seems to be the most natural:

redis.on('error', function (err) {
  // NOTICE: Enters here
  console.log("Error " + err);
})

return redis
  .getAsync(key)
  .then((value) => {
    redis.quit();
    return value;
  })
  .catch((error) => {
    // NOTICE: Never enters here
  });

As I wrote in the comments, I expect the error to be catched in the Promise catch method.
But it is never called. Maybe I am missing something. :/

Thank you guys!

question

Most helpful comment

Here is the response I received from @BridgeAR on Gitter:

Using promises for connection errors is not possible. The issue is, that the connection might be dropped any time and this has to be returned to the user. If you have a idea how to improve this over using the retry_strategy: I'm open for suggestions

>All comments

Here is the response I received from @BridgeAR on Gitter:

Using promises for connection errors is not possible. The issue is, that the connection might be dropped any time and this has to be returned to the user. If you have a idea how to improve this over using the retry_strategy: I'm open for suggestions

Was this page helpful?
0 / 5 - 0 ratings

Related issues

Atala picture Atala  路  3Comments

michaelwittig picture michaelwittig  路  3Comments

id0Sch picture id0Sch  路  4Comments

jackycchen picture jackycchen  路  4Comments

betimer picture betimer  路  5Comments