Possible bug in Domain #7073

Closed
ix-xerri opened this Issue Feb 7, 2014 · 2 comments

Comments

Projects
None yet
4 participants

ix-xerri commented Feb 7, 2014

I have an express server running. Normal exceptions are caught by express. If however an exception is thrown inside a setInterval function, that bubbles up. To avoid this I use domains as follows.

app.use(function(req, res, next) {
    var d = domain.create();
    d.on('error', function(err) {
        console.error(err.stack);
        res.send(500);
        throw new Error('test exception');
    });

    d.add(req);
    d.add(res);
    d.run(function() {
    app.router(req, res, next);
    });
});

All works fine but notice that test exception when that is thrown, the domain module catches it and emits an exit but the error stack shown in the console is not the one of test exception but the one of err (the exception that triggered the on 'error' in the first place.

othiym23 commented Feb 7, 2014

  1. Don't throw from your error handler; the behavior when you do so is undefined. Because Isaac designed domains with the intention of them being used to kill processes in an errored state, there's not really a provision for rethrowing / intercepting errors on the way to being handled somewhere else. Each error will be handled by at most one domain.
  2. It's a lot easier just to do this:
app.use(function(req, res, next) {
    var d = domain.create();
    d.on('error', function(err) {
        console.error(err.stack);
        res.send(500);
    });

    d.add(req);
    d.add(res);
    d.run(next);
});

I think there's a middleware floating around that includes exactly this.

diasdavid added the domains label Feb 14, 2014

Ended up using https://github.com/brianc/node-domain-middleware
Nothing fancy but it keeps the code in a seperate middlware.

jasnell closed this Jun 24, 2015

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment