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!
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
Most helpful comment
Here is the response I received from @BridgeAR on Gitter: