3

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.

asked Mar 17, 2012 at 8:36

1 Answer 1

2

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
Phillip Senn
47.8k92 gold badges263 silver badges381 bronze badges
answered Mar 17, 2012 at 8:39
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.