connect-domain icon indicating copy to clipboard operation
connect-domain copied to clipboard

suggestion: on.error handler improvements

Open binarykitchen opened this issue 12 years ago • 0 comments

hello there

i think the recommended function dispose() is not called on an error. let me explain, you have this line in your code:

reqDomain.on('error', next);

this is fine, you're delegating error handling to the middleware. but there is no guarantee that the middleware is going to render the error properly nor it's going dispose of the error there. an error might be thrown there again for any reasons.

i recommend to improve this code line further to make it more stable:

reqDomain.on('error', function(err) {
    try {
        next(err);
    } catch(exc) {
        console.error('Sorry, your middleware could not handle the error %s', err);
        console.error('Reason: %s', exc);
        // tried our best. clean up anything remaining.
        reqDomain.dispose();
    }
});

this is also recommend for another reason: to avoid memory leaks within nested domains.

my code suggestion is part of the official documentation: http://nodejs.org/docs/latest/api/domain.html

cheers michael

binarykitchen avatar Feb 19 '13 03:02 binarykitchen