2

I have an HTML5 offline application that works rather well, the cache validates, and it works in general, until I actually force the device (or computer) offline.

I have a handler attached to window.applicationCache.onerror so I can handle and present a prompt for any random errors that may occur:

window.applicationCache.onerror = function (e) {
    console.log(JSON.stringify(e));
    model.errorInfo(JSON.stringify(e));
    model.cacheError(true);
};

However, this error handler also fires when the device is offline, which throws up the error dialog, when it really shouldn't.

The JSON this spits out is as follows:

{"cancelBubble":false,"returnValue":true,
 "srcElement":{"onobsolete":null,"status":1},"defaultPrevented":false,
 "timeStamp":1351875347729,"cancelable":false,"bubbles":false,
 "eventPhase":2,"currentTarget":{"onobsolete":null,"status":1},
 "target":{"onobsolete":null,"status":1},"type":"error"}

I see nothing here too obvious on what I can look for to handle this paticular case. Should I be ignoring errors based on something in here, or doing something else entirely?

Matt Sieker
  • 9,349
  • 2
  • 25
  • 43

1 Answers1

1

...this error handler also fires when the device is offline, which throws up the error dialog, when it really shouldn't.

This is actually the appropriate event for this situation, as applicationCache is unable to obtain the manifest and therefore act on a possibly updated manifest. It would be nice to have another event for this situation, but right now there isn't one.

If you wanted to display useful information to the user about cache status: Check applicationCache.status every time the error event fires to see if there was an error on updating a previously-cached app (applicationCache.status===1) or on initial caching (applicationCache.status===0).

ampersand
  • 4,264
  • 2
  • 24
  • 32
  • 1
    You know, I read the spec, and I missed that part, although I see it now. Although, how should something like a server error be handled on an already cached page (e.g. the server throws out a 500 when the client is downloading an updated manifest?) – Matt Sieker Nov 06 '12 at 16:27
  • 1
    The spec states that the [cache failure steps](http://www.whatwg.org/specs/web-apps/current-work/multipage/offline.html#cache-failure-steps), which include firing the `error` event, must be run when there is any 4xx or 5xx error or equivalent. It would be nice if the API presented the status code to the `applicationCache` object, but right now it just lumps all the various errors, whether they are networking or parsing errors under one `error` event. Perhaps someday this will be improved. – ampersand Nov 06 '12 at 20:47