Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Error Handler Nodejs Redis Connect-domain fail

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

like image 990
teddy Avatar asked Jul 19 '26 18:07

teddy


1 Answers

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.

like image 199
inspirationCloud Avatar answered Jul 22 '26 10:07

inspirationCloud