Skip to content

Instantly share code, notes, and snippets.

@Pita
Created December 3, 2011 22:29
Show Gist options
  • Save Pita/1428347 to your computer and use it in GitHub Desktop.
Save Pita/1428347 to your computer and use it in GitHub Desktop.
Async Stacktrace
/*
$ node errortest.js
/home/pita/Desktop/errortest.js:46
throw err;
^
Async Stacktrace:
at /home/pita/Desktop/errortest.js:56:9
at /home/pita/Desktop/errortest.js:66:8
at /home/pita/Desktop/errortest.js:77:8
Error
at Timer.callback (/home/pita/Desktop/errortest.js:87:14)
*/
function ERR(err, callback)
{
if(err != null)
{
if(err.stack != null)
{
var asyncStackLine = new Error().stack.split("\n")[2];
var stackParts = err.stack.split("\n");
if(stackParts[0] != "Async Stacktrace:")
{
stackParts.unshift("Async Stacktrace:","");
}
stackParts.splice(1,0,asyncStackLine);
err.stack = stackParts.join("\n");
}
else
{
err = new Error(err);
}
if(callback != null)
{
callback(err);
}
else
{
throw err;
}
}
return err != null;
}
function one()
{
two(function(err){
if(ERR(err)) return;
console.log("two finished");
});
}
function two(callback)
{
three(function(err)
{
if(ERR(err, callback)) return;
console.log("three finished");
callback();
});
}
function three(callback)
{
four(function(err)
{
if(ERR(err, callback)) return;
console.log("four finished");
callback();
});
}
function four(callback)
{
setTimeout(function(){
callback(new Error());
}, 0);
}
one();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment