Question

to avoid node.js callback hell, i am using async.js to organize my code. its using one callback function at the end to catch all the error. is it possible to find out where it throws the exception.

in following example, there are 3 functions in the array, i want to know where it fails, so I can create a customized error message.

async.waterfall([
    function(callback){
        callback(null, 'one', 'two');
    },
    function(arg1, arg2, callback){
        callback(null, 'three');
    },
    function(arg1, callback){
        // arg1 now equals 'three'
        callback(null, 'done');
    }
], function (err, result) {
   // result now equals 'done' 
       console.log("error:" + err);   
});
Was it helpful?

Solution

In the final callback, err will be the first non-null error value passed in as the first parameter to the callback calls in your waterfall functions. So as long as you make the error values passed to callback different for your three functions you can differentiate them.

However, you mention exceptions which don't figure into this at all as those will simply crash your program. I'm not sure if you meant actual exceptions or were just generally referring to errors as exceptions.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top