Alexander
Alexander

Reputation: 7832

monitoring js errors

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.

Upvotes: 6

Views: 1834

Answers (4)

Allan Ebdrup
Allan Ebdrup

Reputation: 142

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

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

Upvotes: 1

user11507241
user11507241

Reputation: 1

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

Upvotes: 0

Fizer Khan
Fizer Khan

Reputation: 92745

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

Upvotes: 1

Daniil
Daniil

Reputation: 5780

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.

Upvotes: 2

Related Questions