Skip to content

Instantly share code, notes, and snippets.

@mantoni
Created September 2, 2013 15:27
Show Gist options
  • Save mantoni/6414085 to your computer and use it in GitHub Desktop.
Save mantoni/6414085 to your computer and use it in GitHub Desktop.
Error.getStackTrace() for IE
(function () {
"use strict";
var callee = "callee"; // Trick JSLint. It hates arguments.callee.
var isStrict = false;
try {
if (!arguments[callee]) {
throw new Error();
}
} catch (e) {
isStrict = true;
}
function trace(stack, caller) {
var s = caller.toString();
var m = s.match(/function ([^(]+) ?\(/);
var name = m ? m[1] : "<anonymous>";
stack.push(name + " ()");
if (caller.caller) {
if (caller.caller === caller) {
stack.push("<recursion>");
return;
}
if (stack.length >= 50) {
stack.push("<snip>");
return;
}
trace(stack, caller.caller);
}
}
Error.isStrict = isStrict;
Error.prototype.getStackTrace = function () {
var name = this.toString();
if (this.stack) {
if (this.stack.indexOf(this.name) !== 0) {
return (name + "\n" + this.stack).split("\n").join("\n ");
}
return this.stack;
}
var stack = [name];
if (!isStrict) {
trace(stack, arguments[callee].caller);
}
return stack.join("\n ");
};
}());
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment