Aleksander Blomskøld
Aleksander Blomskøld

Reputation: 18542

Complete stack trace with TraceKit javascript library

I'm trying out TraceKit to automatically report Javascript errors to the server. It works as it should, with the exception of the stack trace. It only contains a single element:

TraceKit.report.subscribe(function(stackInfo) { alert(stackInfo.stack.length);});

function foo() {
   bar();
}

function bar() {                                                
   throw "oops";
}

foo();  

The alert shows '1'. I've also created a JSFiddle with the same code;

Why don't I get the complete stack trace? I've tested with both Chrome and Firefox.

Upvotes: 4

Views: 2568

Answers (1)

Devin Rhode
Devin Rhode

Reputation: 25267

For now, you'll want to change throw "oops"; to throw new Error("oops"); you should then get a full stack trace.

In the future, TraceKit can fix this by re-throwing an error and fixing the stack trace accordingly. This jsfiddle shows re-throwing to get a real stack trace: http://jsfiddle.net/DevinRhode2/dAYmJ/1/

The code there is:

try {
  function foo() {
     bar();
  }

  function bar() {                                                
     throw "oops";
  }

  foo();
} catch (e) {
  console.log(e, e.message, e.stack); //oops undefined undefined 
  try {
    throw new Error(e);
  } catch (e) {
    console.log(
      e,       e.message, e.stack);/*
      Error {} "oops"     "Error: oops
        at file://localhost/Users/devinrhode2/Desktop/test.html:23:13"
    */
  }
}​​​​​​​

Other discussion on TraceKit on github here: https://github.com/occ/TraceKit/issues/23

Thanks Alex for bringing this up!

Upvotes: 4

Related Questions