5

I have a simple program, which needs to make sure I can connect to a Redis server. I use node-redis to connect and need to wait until Redis is started. I use this piece of code:

function initializeRedis(callback) {
    (function createClient(){
        var runner;
        try {
            client = redis.createClient();
        } catch (e) {
            setTimeout(createClient, 1000);
        }
        callback();
    })();
};

initializeRedis(function() {
// Work here
});

This is because without the try/catch, I got an exception from node.js:

 node.js:134
         throw e; // process.nextTick error, or 'error' event on first tick
         ^ Error: Redis connection to 127.0.0.1:6379 failed - ECONNREFUSED, Connection refused
     at Socket.<anonymous> (/var/www/php-jobs/node_modules/redis/index.js:88:28)
     at Socket.emit (events.js:64:17)
     at Array.<anonymous> (net.js:830:27)
     at EventEmitter._tickCallback (node.js:126:26)

When I start redis-server (Ubuntu machine) and start this script, everything works fine. If I stop redis-server and start the script, it doesn't catch the exception and still throws this same exception. How is that possible? I have a try/catch statement!

1 Answer 1

12

After client = redis.createClient();, set a handler for the error event:

client.on('error', function(err) {
  // handle async errors here
});

Have a look at the stack trace - your code isn't in it, so there's no place where a try/catch could catch the error.

Sign up to request clarification or add additional context in comments.

6 Comments

If the error is asynchronous, how would I be able to wait until I have a valid connection? The problem in my startup cycle is this node script is started with upstart and redis with init.d. Apparently, node starts earlier and I have to wait until redis is ready too. That's what I am trying with the setTimeout() and callback here.
@JurianSluiman: You don't wait for it, you listen for it. And don't use setTimeout for this kind of stuff, it makes kittens DIE!
obviously the setTimeout() won't work with this client.on('error') handler, but the problem is I need to WAIT before I can continue because I DEPEND on the client. The callback() is triggered now because no exception is thrown and AFTER that I probably get this error triggered. Aynchronously processing errors sucks because if you depend on the result, you can't really do anything about that?
@JurianSluiman: huh, what? I don't get what you're trying to say.
TL;DR: I cannot just "wait" asynchronously for the error because I synchrously depend on the client. Your callback handle is therefore no solution. See my question about the waiting part here: stackoverflow.com/questions/7786660/…
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.