Created
September 2, 2013 15:27
-
-
Save mantoni/6414085 to your computer and use it in GitHub Desktop.
Error.getStackTrace() for IE
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(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