2

I tried to use connect-domain to handling error. In most cases it ok, but it fail with redis callback. How to fix this?

Here's my app

var http = require('http');
var express = require('express');
var connectDomain = require('connect-domain');
var redis = require("redis").createClient();

var app = express();
app.use(connectDomain());

app.get('/', function (req, res) {
    throw new Error("Handler OK");
});

app.get('/error', function (req, res) {
    redis.get("akey", function(err, reply) {
        throw new Error("Handler error");
        res.end("ok");
    });
});

app.use(function (err, req, res, next) {
    res.end(err.message);
});

http.createServer(app).listen(8989, function() {
    console.log("Express server started ");
});

I use nodejs 0.8.16, all modules are latest

2 Answers 2

10

Not sure if the domain should be catching that or not - but you can capture redis errors by setting up an error handler, like this:

// handle redis connection temporarily going down without app crashing
redisClient.on("error", function(err) {
    console.error("Error connecting to redis", err);
});

While the connection is broken your handler will keep getting called as redis tries to reconnect. If it's eventually successful everything will come back online on it's own.

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

1 Comment

My problem must be solve for production use, of course. Because this problem, I have spend a lot effort to catch exception to ensure that the system isn't terminated on production mode.
0

You can also try https://www.npmjs.org/package/wait-for-redis. It ensures clients can wait for server to be up in case when clients start early.

Comments

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.