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.