1

Right now we are working on a way to catch errors that occur in JavaScript. We have everything working in browsers including and after IE9.

In modern browsers we can wrap window.EventTarget.prototype.addEventListener with a function that does a try/catch in order to capture errors. For example we can do this:

var addEventListener = window.EventTarget.prototype.addEventListener;

window.EventTarget.prototype.addEventListener = function (event, callback, bubble) {
return addEventListener.call(this, event, WATCH_FUNCTION(callback), bubble)
};

The WATCH_FUNCTION above has our try/catch in it. Right now we cannot figure out a way to wrap the attachEvent that exists on an element in IE8. For example:

var myObject = document.getElementById('my-id');

myObject.attachEvent('onclick', function () {reference-to-undefined-var});

Our hope is to wrap the error that is going to be raised by the attached event. Right now we cannot figure out how to always wrap attachEvent. We will be a third party library so we cannot force people to use a different form of attaching events.

As a note for anyone who looks at this question I have tried to override the following and none seem to work:

  • Object.prototype.attachEvent
  • Element.prototype.attachEvent
  • window.attachEvent
Garry Polley
  • 4,253
  • 1
  • 22
  • 29
  • Why not use the onerror event? – Mouser Jan 20 '15 at 21:23
  • The `onerror` only captures events that are fired at that level. If someone has IE setup in a development way it will not capture events at the level. http://stackoverflow.com/questions/1915812/window-onerror-does-not-work Also the stacktrace and information available at `onerror` are not as detailed as when you do a `try/catch`. I am using `onerror` as well. I want to get as much information as possible. – Garry Polley Jan 20 '15 at 21:30
  • Looking at this more if someone knows how to override methods (from a global perspective) of the IHTMLElement2 that would work too. https://msdn.microsoft.com/en-us/library/ie/aa703984(v=vs.85).aspx – Garry Polley Jan 20 '15 at 22:21
  • I've gotten a little further. By accessing `window.Element.prototype.attachEvent` mine is now getting called. However, IE8 is giving me a fuss about: "invalid procedure call or argument". The reason I think `Element` failed before was due to a page messing with Element directly. – Garry Polley Jan 21 '15 at 14:30
  • No matter how you try there will be gaps in your lib. Better to use just the onerror without the stack and don't care much about old browsers. Is this an open source project btw? I am working on a js error lib for logging errors and parsing the stack. Originally I wanted a V8 polyfill, but it is not possible to do that. At least I don't want to wrap everything like you do, because using try-catch blocks slows down the code afaik. – inf3rno Feb 19 '18 at 12:28
  • Yes the library is open source and maintained here: https://github.com/cerner/canadarm – Garry Polley Mar 26 '18 at 20:55

1 Answers1

1

I finally figured out what I had been doing wrong and the following appears to be working in IE8. For all other browsers you can simply override the window.EventTarget versions. Basically I was missing the use of the call, without that it does not know the context of what's making the call to attachEvent.

var attachEvent = window.Element.prototype.attachEvent,
      detachEvent = window.Element.prototype.detachEvent;

  window.Element.prototype.attachEvent = function (event, callback) {
    return attachEvent.call(this, event, watch(callback));
  };

  window.Element.prototype.detachEvent = function (event, callback) {
    return detachEvent.call(this, event, watch(callback));
  };
Garry Polley
  • 4,253
  • 1
  • 22
  • 29
  • This does work technically. However, if you have any xml based nodes the prototypal inheritance will be broken. Meaning, the attachEvent on the xml node will not work. Specifically a node of the form: – Garry Polley Apr 09 '15 at 17:40