I'm writing an error event handler that captures an error and make an ajax call to the server to report it. The problem is that a lot of times, my code looks like this:
function A() {
//lots of works to calculate SomeParameters; can bug
CalledVeryOften(SomeParameters);
}
function CalledVeryOften(SomeParameters) {
// a little bit of work
}
If I capture the event in function A (or B, C, D...), great! But the problem is that if the window.onerror event fires in CalledVeryOften then it might be that the parameters might not be properly calculated.
Is there a way in CalledVeryOften to determine which function it was called from?
Thanks.
1 Answer 1
The name of the calling function can be obtained via the deprecated arguments.callee.caller.name property MDN: arguments.callee .
Another method is by parsing the value of new Error().stack.
In Chrome you can use:
var stackTrace = {};
Error.captureStackTrace(stackTrace); // Get the stack trace
stackTrace = stackTrace.stack; // Formatted string