Getting the stack trace of an error
When I catch an error, I would like to be able to log its stack trace. So far I’ve come up with this little trick:
Error.prototype.toString = function() {
if (typeof this.stack === "undefined" || this.stack === null) {
this.stack = "placeholder";
// The previous line is needed because the next line may indirectly call this method.
this.stack = $.stack;
}
return "Error";
}
This attaches the stack to any manually-created error. For example, I can run:
try {
throw new Error("I'm an error.");
}
catch (e) {
$.writeln("Stack: " + e.stack);
}
And this will print the correct stack for the error. However, it only works for errors that I create. Is there any way to get the stack trace of any error?
Full disclosure: I’ve asked this on Stack Overflow without much success. See here for more info: http://stackoverflow.com/questions/16201574/getting-the-stack-trace-of-an-error-in-extendscript
