Q:

Trapping AssertionError in nested callback function

I am writing an updated testing library for Node.js and am trying to properly trap errors that occur in test callbacks
for some reason, the following code doesn't trap an AssertionError:
process.on('uncaughtException',function(err){
    console.error(err);  //an instance of AssertionError will show up here
});

[file1,file2,file2].forEach(function (file) {

        self.it('[test] ' + path.basename(file), {

            parallel:true

        },function testCallback(done) {

            var jsonDataForEnrichment = require(file);

            request({

                url: serverEndpoint,
                json: true,
                body: jsonDataForEnrichment,
                method: 'POST'

            }, function (error, response, body) {
                if (error) {
                    done(error);
                }
                else {
                    assert(response.statusCode == 201, "Error: Response Code"); //this throws an error, which is OK of course
                    done();
                }
            });
        });

    });

I handle the callback (I named it "testCallback" above), with this code:
                try {

                    if (!inDebugMode) {
                        var err = new Error('timed out - ' + test.cb);
                        var timer = setTimeout(function () {
                            test.timedOut = true;
                            cb(err);
                        }, 5000);
                    }

                    test.cb.apply({
                        data: test.data,
                        desc: test.desc,
                        testId: test.testId
                    }, [function (err) {   //this anonymous function is passed as the done functon
                        cb(err);
                    }]);
                }
                catch (err) {  //assertion error is not caught here
                    console.log(err.stack);
                    cb(err);
                }

I assume the problem is that callbacks that result from async functions like those made in the request module, cannot be trapped by simple error handling.
What is the best way to trap that error?
Should I just flesh out the process.on('uncaughtException') handler? Or is there a better way?

A:

The best way to handle this appears to be Node.js domains, a core module
https://nodejs.org/api/domain.html
it will likely be deprecated soon, but hopefully there will be a replacement that can have similar functionality, because the domain module is saving my ass right now, as I have no other way to trap errors, because the errors might be generated by my users' code, not my code.

