Javascript Stacktrace update

I started a Javascript Stacktrace project back in August 2008. The idea was to give additional debugging power to browsers where you don’t have good tools to work with. I’d like to give you an update on where the project is today.

Lately, I’ve been working on updating my old script. Since it was written, we’ve seen lots of major browser releases and the introduction of the V8 Javascript engine used by Google Chrome.

Updated browser compatibility

Browsers that are fully-supported and well-tested:

  1. Firefox (and Iceweasel) 0.9+
  2. UPDATE: Chrome 1+ now perfectly supported
  3. Safari 3+
  4. IE 5.5+
  5. Opera 9+
  6. Konqueror 3.5+
  7. K-Meleon 1.5.3+
  8. Epiphany 2.28.0+

Browsers that are supported in almost all cases but not as well-tested:

  1. Chrome 1+ – One bug (feature?) that may be in Chrome reporting functions as anonymous when they aren’t. HOWEVER, Chrome’s stack gives us line numbers AND column numbers, so we can see exactly where our problem is – even in minified Javascript! Sweet! Chrome 1+ now fully supported.
  2. Opera 7-8 – Opera is dead to me now. Opera 10+ has removed the error.stack info we needed and introduced error.stacktrace, but it seems very unstable. Argh.

More info about compatibility can be shown with the BrowserShots of the test suite.

Now socially coded

I’m not going to post the code here because the source and tests are now on the javascript-stacktrace project on GitHub. You can download it here.

Follow it, file bugs, and make comments there. If you have improvements to make, please fork the project and then contact me or do a “push request”. I’ll make sure you get credit ;)

UPDATE: Øyvind Sean Kinsey has added memoization (caching the implementation) for the mode and XHR bits as well as the ability to pass an existing Javascript Error and get a stacktrace. We’re working on tests and you should see project updates soon. Thanks, Øyvind!

var lastError;
try {
    // error producing code
} catch(e) {
   lastError = e;
   // do something else with error
}

// later...
printStackTrace({e: lastError}); //Returns stacktrace from lastError!

Try it out!

The code is in use on my blog. Click here to give it a spin.

function foo() {
    var blah;
    bar("blah");
}

function bar(blah) {
    var stuff;
    thing();
}

function thing() {
    if (true) { //your error condition here
        var st = printStackTrace();
        alert(st.join("\n\n"));
    }
}

foo();

Random note: one cool suggestion I saw was to assign printStackTrace to window.onerror. Pretty brilliant if you ask me.

I want to thank the guys who contributed to the script: Luke Smith, Loic Dachary and Johan Euphrosine.

I could use a bit of help getting the Chrome and Opera bugs worked out. I’m sure some of you guys who remember how to write software can help. Suggestions and whinings are welcome as long as they don’t get out of hand in the comments.

Javascript Stacktrace on GitHub

If you liked this post, please help me share it

Responses (14)

  1. Luke Smith says:

    Glad to help, Eric. Great to see that you’ve kept up with it and posted to GitHub.

  2. [...] This post was mentioned on Twitter by Smashing Magazine, purplehayz, Richard Laksana, Pump Action Cumshot, Kaynat Soft and others. Kaynat Soft said: RT: @smashingmag: JavaScript Stacktrace: give debugging power to browsers – http://bit.ly/95zY6y [...]

  3. Social comments and analytics for this post…

    This post was mentioned on Twitter by rlaksana: Javascript Stacktrace Update – http://su.pr/2fsavN...

  4. Nice one, I will definitively use this
    But one small suggestion, in the createXMLHTTPObject function, you should use memoization to cache the implementation
    {{
    try {
    xmlhttp = XMLHttpFactories[i]();
    createXMLHTTPObject = XMLHttpFactories[i];
    }
    }}

  5. [...] unos días, Eric actualizó el código integrando a Google Chrome entre los navegadores soportados por la [...]

  6. 2ni says:

    Any idea how this works with jquery?
    I have
    jQuery(document).ready( function () { foo['bar']++; });

    and window.onerror = function(msg, url, line) {alert(printStackTrace());}

    But the ready() function seems to swallow the stacktrace…
    Any clue?

  7. P says:

    Opera 10.50’s stack trace in dragonfly is the most impressive i’ve seen, where most quit at the first closure or anonymous, it manages to go all the way down to the root.

  8. DrSoong says:

    Can you also make this great functionality available as a bookmarklet? That would really make it very useable, because you would not need to change the sourcecode on the website.

  9. [...] at generating a stack trace in JavaScript for the purpose of error reporting.  There are plenty of existing projects out there that solve this problem, so I wouldn’t recommend rolling your [...]

  10. Thanks for this Eric!

  11. [...] at generating a stack trace in JavaScript for the purpose of error reporting. There are plenty of existing projects out there that solve this problem, so I wouldn’t recommend rolling your [...]

Leave a Reply