Wednesday, April 7, 2010

XHR.onreadystatechange and exceptions

Generally it is not good idea to throw exception in event handler.
However people are lazy and bugs happen, so let's see out chances in this situation.

xhr.onreadystatechange=function(){throw new Error();}

MSIE and FF supports window.onerror event, so uncought exceptions can end up in
this global handler.

All browsers support some sort of "display JavaScript errors" but usually well hidden
as small icon on status bar (MSIE) or deeply in menus (other browsers).

Uniform handling is almost impossible. The very first idea was to rely on
window.error in MSIE an FF and call window.onerror explicitly in
other browsers (even if the browser does not support this error you can define

window.onerror=function...
and call it using window.onerror(msg,..,...) syntax.

However situation is even worse.

  1. FF 3.5.6 works fine, ewrror thrown, ends in window.onerror as expected.
  2. MSIE 7.0 NativeXHR + 200 response - works fine
  3. MSIE 7.0 NativeXHR + conditional request + 304 response - exception lost, window.onerror not called
  4. MSIE 7.0 NativeXHR + cached version not tested byt expected - exception lost, window.onerror not called
  5. FF 3.6 exception lost, window.onerror not called
So it seems that we cannot rely:
  1. exception being thrown out of boundaries of the handler (eaten exception)
  2. even if thrown to be catched somewhere (missing window.onerror concept)
P.S. tested in async true scenarios, async false can reveal more troubles....

No comments:

Post a Comment