Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

With Firefox/Firebug, Why do You Only Get Stacktraces With Some Errors

I've noticed that with Firefox/Firebug (my main development environment), some errors produce stack traces, while others don't. At first I thought this had something to do with whether I generated the error (eg. new Error("foo")) or whether Javascript generated it (eg. undefinedFoo += 1), but I've noticed it happening with both types of errors.

So, my question is, does anyone know what causes this? Is it random or is there some pattern/logic to it? Is it just Firefox and/or Firebug being lame or do other browsers do this too? And most importantly, is there anything I can do to control it (specifically to make it always give me stack traces)?

This isn't a crucial question (as I can always throw a "console.trace()" just before whatever line the error occurs on), but I'm curious if anyone knows the answer.

**********EDIT**********

I found this thread: http://groups.google.com/group/firebug/browse_thread/thread/1f32df8b96ec1d30/64b9074cb99056c2?pli=1 which mentions that Firefox doesn't provide Firebug with stacktraces if the error isn't an instance of Error. In other words, if you throw something that isn't new Error("foo"), you won't get a stacktrace.

However, this doesn't help me, as I'm not seeing stacktraces even when I throw new Error().

like image 528
machineghost Avatar asked Nov 05 '22 02:11

machineghost


1 Answers

I found it! It turns out the module pattern is the problem. Here's a simple example:

var OuterModule = (function(module){
    module.throwError = function() {
        throw new Error("This has no stack trace!");
    };
    return module;
})({});
OuterModule.throwError();

For some reason, because the module pattern hides throwError function inside the module's scope, Firebug/Firefox is unable to trace it properly.

Man that took forever too figure out; hopefully someone else will benefit from my suffering :-)

like image 148
machineghost Avatar answered Dec 24 '22 08:12

machineghost