Sangram Singh
Sangram Singh

Reputation: 7191

catch connection error throw(n) from redis/index.js file

I am connecting to redis thru heroku.

var redisClient = require('redis').createClient({
    host: 'http://networkinglawyer.in/home/redis',
    port: 9374,
    db: 0,
    requirepass: 'abcdefghijklmnopqrstuvwxyz'
  });

The redis.index.js file is throwing err how do i catch to print it onto console?

Edit:- Error is

2013-11-29T07:18:38.255695+00:00 app[web.1]: userSchema defined
2013-11-29T07:18:38.262109+00:00 app[web.1]: questionSchema defined
2013-11-29T07:18:38.264414+00:00 app[web.1]: activitySchema defined
2013-11-29T07:18:38.273814+00:00 app[web.1]: connection error: [Error: failed to connect to [localhost:27017]]
2013-11-29T07:18:38.282107+00:00 app[web.1]: 
2013-11-29T07:18:38.282867+00:00 app[web.1]:                 throw callback_err;
2013-11-29T07:18:38.282528+00:00 app[web.1]: /app/node_modules/redis/index.js:563
2013-11-29T07:18:38.283575+00:00 app[web.1]:                       ^
2013-11-29T07:18:38.287475+00:00 app[web.1]:     at RedisClient.on_info_cmd (/app/node_modules/redis/index.js:371:35)
2013-1

Upvotes: 0

Views: 2054

Answers (2)

hgoebl
hgoebl

Reputation: 13007

You have an error in a callback-function which is called by redis:

// from https://github.com/mranney/node_redis/blob/master/index.js
if (command_obj && typeof command_obj.callback === "function") {
    try {
        command_obj.callback(err);
    } catch (callback_err) {
        // if a callback throws an exception, re-throw it on a new stack so the parser can keep going
        process.nextTick(function () {
            throw callback_err;
        });
    }
}

The real problem is that you cannot connect to mongodb and this throws an exception in a callback function invoked by redis-client.

Upvotes: 1

Tom Grant
Tom Grant

Reputation: 2045

To catch the error and print it to the console:

redisClient.on('error', function (err) {
    console.log('Error ' + err);
});

You may also want to add handling of different forms of errors in later as well.

Upvotes: 1

Related Questions