7

I came across this method to produce a Javascript stack trace (to fix an IE specific bug): http://pastie.org/253058.txt which sounds really useful, but when I call it, the stack trace I get is for the code of the script itself?!

Can this code be changed to produce a general stack trace? Or is there a better way to get a stack trace in IE?

(function () {

YOUR_NAMESPACE.getStackTrace = (function () {

var mode;
try {(0)()} catch (e) {
    mode = e.stack ? 'Firefox' : window.opera ? 'Opera' : 'Other';
}

switch (mode) {
    case 'Firefox' : return function () {
        try {(0)()} catch (e) {
            return e.stack.replace(/^.*?\n/,'').
                           replace(/(?:\n@:0)?\s+$/m,'').
                           replace(/^\(/gm,'{anonymous}(').
                           split("\n");
        }
    };

    case 'Opera' : return function () {
        try {(0)()} catch (e) {
            var lines = e.message.split("\n"),
                ANON = '{anonymous}',
                lineRE = /Line\s+(\d+).*?in\s+(http\S+)(?:.*?in\s+function\s+(\S+))?/i,
                i,j,len;

            for (i=4,j=0,len=lines.length; i<len; i+=2) {
                if (lineRE.test(lines[i])) {
                    lines[j++] = (RegExp.$3 ?
                        RegExp.$3 + '()@' + RegExp.$2 + RegExp.$1 :
                        ANON + RegExp.$2 + ':' + RegExp.$1) +
                        ' -- ' + lines[i+1].replace(/^\s+/,'');
                }
            }

            lines.splice(j,lines.length-j);
            return lines;
        }
    };

    default : return function () {
        var curr  = arguments.callee.caller,
            FUNC  = 'function', ANON = "{anonymous}",
            fnRE  = /function\s*([\w\-$]+)?\s*\(/i,
            stack = [],j=0,
            fn,args,i;

        while (curr) {
            fn    = fnRE.test(curr.toString()) ? RegExp.$1 || ANON : ANON;
            args  = stack.slice.call(curr.arguments);
            i     = args.length;

            while (i--) {
                switch (typeof args[i]) {
                    case 'string'  : args[i] = '"'+args[i].replace(/"/g,'\\"')+'"'; break;
                    case 'function': args[i] = FUNC; break;
                }
            }

            stack[j++] = fn + '(' + args.join() + ')';
            curr = curr.caller;
        }

        return stack;
    };
}

})();
2
  • What do you mean "for the code of the script itself"? The call to the stack trace code should be at the top of the stack trace, but then under that is your caller, right? Commented Oct 1, 2009 at 14:47
  • Hi Jeremy, That may be the case. I'm displaying the text using alert(), but the text I'm getting back ends in '...' so maybe it's too big and the actual trace is just below this. Is it possible to remove the 'trace function' call from the output? Commented Oct 1, 2009 at 16:10

2 Answers 2

12

This getStackTrace() function creates the stack trace of the function from which you've called getStackTrace(). It does not create the stack trace of an error that you've caught. For example, you'd use it to try to figure out how a specific function is being called:

function foo() {
    // debug how this is being called
    alert(YOUR_NAMESPACE.getStackTrace());
}

Or to add some more detail to an error you raise:

function foo() {
    // signal something went wrong
    var error = new Error("error in foo");
    if (!error.stack)
        error.stack = YOUR_NAMESPACE.getStackTrace();
    throw error;
}

You can not use it like this:

try {
    foo();
} catch (e) {
    alert(YOUR_NAMESPACE.getStackTrace(e));
}

Here's a good rundown of what stack information you can get -- and from which browsers -- when an error occurs: Three Painful Ways to Obtain a Stack Trace in Javascript (Archive.org link replacing dead link)

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks Justin, this is what I was looking for. The problem appears to be the way I was calling it. Thanks a lot.
-1

You might be better off using IE 8's built-in debugger.

3 Comments

Thanks for your suggestion. Unfortunately, i'm stuck with IE7 (corporate network and all that).
Even with IE7 you can turn javascript debugging on in internet options and launch visual studio on error.
Thanks for your suggestion Mike. Unfortunately, due to the same network restrictions I can't enable the Javascript debugger in IE.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.