Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

Already on GitHub? Sign in to your account

Handling async errors/exceptions using node's new feature - domain #1291

jayyvis opened this Issue Aug 17, 2012 · 10 comments


None yet
5 participants

jayyvis commented Aug 17, 2012

If any of the middleware throws an error, express catches and forwards it to error handling middlewares. It works well with synchronous code but once it goes async and an error is thrown, express will not be able to catch it.

I can understand that it's due to the limitation with node where you can't catch an thrown from an async callbacks. Only option is to use process.on('uncaughtException') but that will not have any context and hence useless.

With the introduction of node's domain objects, I think now we can handle errors thrown from an async callback and route it to error handling middleware's.


How about making express to execute every request under a domain and handle both sync & async errors thrown ?


tj commented Aug 17, 2012

I'd rather wait until people play around with them some more, they have weird edge-cases and it's a sketchy implementation to begin with

jayyvis commented Aug 17, 2012

You're correct TJ. Thank you for blazing fast response.


tj commented Aug 28, 2012

closing for now

@tj tj closed this Aug 28, 2012

I created a little middleware for connect/express that implemets domain functionality. Here is it: connect-domain. It works fine for me, and it very useful because error handler still have request and response contexts. I have no problems with it my production. You can try it.

jayyvis commented Sep 18, 2012

Interesting baryshev. It's really nice idea to have it as a custom middleware for now. I'll give it a try & update you.


tj commented Sep 18, 2012

cool thanks @baryshev, anyone reading this let me know how you find domains haha. I know there isn't much adoption yet in the community, I'm skeptical myself but I haven't had time to use them much, certainly not in production yet

baryshev commented Dec 7, 2012

I just released little update for connect-domain middleware. Now we can handle asynchronous errors like regular errors with connect error handler.

Simple example:

    connect = require('connect'),
    connectDomain = require('connect-domain');

var app = connect()
    .use(function(req, res){
        if (Math.random() > 0.5) {
            throw new Error('Simple error');
        setTimeout(function() {
            if (Math.random() > 0.5) {
                throw new Error('Asynchronous error from timeout');
            } else {
                res.end('Hello from Connect!');
        }, 1000);
    .use(function(err, req, res, next) {


rlidwka commented Dec 15, 2013

Any news about this?

I don't quite like the way how synchronous errors are catched, but asynchronous errors aren't, so I'd appreciate if that difference will be eliminated one way or the other.


jonathanong commented Dec 15, 2013

out of scope of express IMO. just create a module that does this, or wrap your entire server/app in it: http://nodejs.org/api/domain.html#domain_explicit_binding


rlidwka commented Dec 15, 2013

I wonder why implicit try..catch is well within the scope of express, but implicit domain binding isn't. Considering the fact that it is essentially the same thing.

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