-
Notifications
You must be signed in to change notification settings - Fork 280
Description
I tested this in at least Firefox and Chrome, and the stack trace is completely uninformative if called from window.onerror. I guess this makes sense, since by the time the error has reached the onerror handler, the stack has already been unrolled and discarded. Since there's no error object that can be used, printStackTrace creates it's own error object. Since this is now the new point where the error has been thrown, and since no previous stack trace is available anyway, the stack trace is now filled with only stack frames from within the stacktrace.js function.
If I'm doing something something wrong and don't know about it, please let me know.
My code:
window.onerror = function() {
alert(printStackTrace({guess:true}).join("\n");
}
function a() {
b();
}
function b() {
c();
}
a(); // Throws an error since c is undefined
Output in Firefox:
createException()@http://localhost:4000/stacktrace.js:42
run(null)@http://localhost:4000/stacktrace.js:27
printStackTrace([object Object])@http://localhost:4000/stacktrace.js:18
onerror("c is not defined","http://localhost:4000/test.js",6)@http://localhost:4000/test.html:5
Note that the last line in the trace above seems to be not from the stack, but from the arguments of the window.onerror handler, which I could have got anyway without printStackTrace by inspecting the arguments object in the handler. There's no mention of a or b that lead up to c.
Output in Chrome:
Object.createException (http://localhost:4000/stacktrace.js:42:18)
Object.run (http://localhost:4000/stacktrace.js:27:25)
printStackTrace (http://localhost:4000/stacktrace.js:18:62)
onerror()@http://localhost:4000/test.html:5:11
I tried moving a, and b to a separate js file included with a <script>
tag, to see if {guess:true}
will be able to help by reading the source, but that didn't help.
Am I doing something wrong?