6

I am interested in monitoring javascript errors and logging the errors with the callstack.

I am not interested to wrap everything in try-catch blocks.

According to this article http://blog.errorception.com/2011/12/call-stacks-in-ie.html it's possible inside window.onerror "recursively call .caller for each function in the stack to know the previous function in the stack"

I tried to get the callstack:

window.onerror = function(errorMsg, url, lineNumber)
{
    var stk = [], clr = arguments.callee.caller;
    while(clr)
    {
        stk.push("" + clr);
        clr = clr.caller;
    }
    // Logging stk
    send_callstack_to_log(stk);
}

but only one step is possible even if the callstack was much longer:

(function()
{
function inside() {it.will.be.exception;};
function middle() {inside()};
function outside() {middle()}
outside();
})();

One step isn't interesting because onerror arguments give me even more information about it.

Yes, I tried it with IE according the article I mentioned above.

Remark: I also tried to open an account on "ERRORCAEPTION" to gather error log. I tested it with IE and "ERRORCAEPTION" recognize that the errors are coming from IE, but I can't find any callstack information in the log I've got there.

2
  • You might take a look at stacktrace.js. It is a cross-browser solution. Commented Mar 17, 2013 at 14:17
  • 1
    Quick update for people stumbling on this in 2019+: This isn't really a problem anymore. All common modern browsers are capable of producing a reliable stack trace, and the usage of callee on functions is restricted. You can generate a stack with either console.trace or by throwing and catching an error. If you want to see when errors happen in production, services like TrackJS Error Monitoring can do that. Commented Aug 29, 2019 at 20:26

4 Answers 4

2

Unfortunately this log will not always be available, it lacks line numbers, you can not really rely on it.

Try https://qbaka.com

Qbaka automatically overload bunch of JavaScript functions like addEventListener, setTimeout, XMLHtppRequest, etc so that errors happening in callbacks are automatically wrapped with try-catch and you will get stacktraces without any code modification.

Sign up to request clarification or add additional context in comments.

Comments

1

You can try atatus which provides javascript contextual error tracking: https://www.atatus.com/

Comments

1

Take a look here: https://github.com/eriwen/javascript-stacktrace

That's the one I use on Muscula, a service like trackjs.

1 Comment

I've seen this one. It doesn't help me inside window.onerror. Inside try-catch it's easy to get the stack, but as I told in my question - I can't wrap everything in try-catch blocks. But thank you for your effort.
0

I have wrote a program to monitor js error. maybe it will help.

I used three kind of methods to catch exceptions, such as window.onerror, rewrite console.error and window.onunhandledrejection. So I can get Uncaught error, unhandled promise rejection and Custom error

Take a look here: https://github.com/a597873885/webfunny_monitor

or here: https:www.webfunny.cn

It will be help

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.