Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Loading…

Possible bug in Domain #7073

Open
ix-xerri opened this Issue · 2 comments

3 participants

@ix-xerri

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
  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 diasdavid added the domains label
@ix-xerri

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.