Stacktraces and JavaScript

Stack traces are available by default in most modern browsers

Most modern browsers provide stack traces without having to do any extra work. Errorception makes the most of this, and gives you stack traces when possible.

The information here is not Errorception-specific, and is based on emerging web standards.

Error objects

Traditionally, in JavaScript, you only get a stack trace if you have an Error object. error.stack (or some similar property depending on the browser) can be accessed to get at the stack trace.

This means that when you send errors to Errorception using _errs.push, since you are handing over an Error object to Errorception, you will have access to the stack trace in most cases.

Browser support: All browsers except older versions of IE provide a stack trace if you have an error object. In IE <10, it is simply impossible to get stack traces in Internet Explorer, using any method at all.

This doesn't stop Errorception from trying to get stack traces, of course. :) Errorception tries its best even in older IEs, to build a stack trace. Read more about call stacks in IE.

window.onerror

Errorception uses window.onerror as its primary mechanism of catching errors. However, traditionally, window.onerror hasn't received an Error object. As a result, window.onerror hasn't had access to stack traces, and has suffered a bad rap as a result.

However, there was recently a change made to the HTML spec to pass a valid Error object to window.onerror. Errorception fully supports this, and uses the error object to extract stack traces.

Browser support: Chrome and Firefox rolled this out in their public builds around November 2013. That's ages ago in browser years! IE doesn't support this yet, but I've been keeping an eye on them, and I'm confident that the next release of IE will provide error objects. Webkit is still to catch up here, and stack traces aren't currently available with window.onerror in Webkit.

What should you do?

When you copy/paste Errorception's snippet to your site, Errorception uses the window.onerror approach by default. In addition to this, you could also send errors to Errorception using _errs.push, which will also give you stack traces in your errors. However, it's admittedly some extra work. Is it worth it?

I recommend that you start with the vanilla window.onerror approach — the one that Errorception provides by default when you copy/paste the script snippet. If you come across an error that absolutely needs a stack trace to debug, but doesn't already have one, you could consider using _errs.push for just that part of your code.

That way, neither do you need to make any code changes preemptively, nor does Errorception need to wrap all browser/library native methods to get error objects. It's worth considering that Errorception deduplicates errors rather intelligently. So, if any one error occurrence has a stack trace, you already have what you want. When all of this fails, and the error isn't in IE <10, is when I'd consider using _errs.push.