user1031947
user1031947

Reputation: 6664

Connect-Domain does not catch error in the following situation:

I am using the connect-domain module (https://github.com/baryshev/connect-domain) to centralize error handling in my Express application.

For the most part it works. However, for reasons I don't understand, when I throw an error inside of a fs.exists check, it does not catch the error and instead crashes node.

app.get( "/anurl/", function( req, res ){
    ...
    fs.exists( filename, function( exists ) {
        if ( !exists ) throw new Error( "bah!" );
        ...
    });
});

EDITED:

After quite a bit of testing, I have learned that the above is not the true cause of the problem.

The actual problem is related to using Redis as a session store:

app.use( connectDomain() );
app.use( express.session({
    secret: "secretz",
    store: new RedisStore({ client: redis })
}));

Using the above, connectDomain no longer works for any errors that are thrown asynchronously. (This includes calls to fileSystem, timeOuts, database connections, etc.)

If I change the above to the following...

app.use( connectDomain() );
app.use( express.session({ secret: "secretz" }));

...then everything works perfectly.

So something about RedisStore is breaking Connect-Domain. Unfortunately I need to use Redis, to persist my sessions.

Any further advice on how to fix this would be much appreciated.

Upvotes: 0

Views: 499

Answers (2)

Vadim Baryshev
Vadim Baryshev

Reputation: 26199

I just tryed with this code:

var http = require('http');
var express = require('express');
var connectDomain = require('connect-domain');
var fs = require('fs');

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

app.get('/', function (req, res) {
    fs.exists('test', function (err) {
        if (!err) throw new Error('bah!');
        res.writeHead(200, {'Content-Type': 'text/plain'});
        res.end('Hello World\n');
    });
});

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

http.createServer(app).listen(1339, '0.0.0.0');

Error was successfully catched.

node.js: 0.10.1

connect-domain: 0.5.0

Upvotes: 2

MeTitus
MeTitus

Reputation: 3428

See if it is related with the:

"throw vs throw new" thing

http://www.javascriptkit.com/javatutors/trycatch2.shtml

Upvotes: 0

Related Questions