0

I am having an issue where an XHR error is not properly caught. I am using the Offline.js library to do some checking.

The code the library is performing is:

Offline.checks.xhr = function() {
  var e, xhr;
  xhr = new XMLHttpRequest;
  xhr.offline = false;
  xhr.open(Offline.getOption('checks.xhr.type'), Offline.getOption('checks.xhr.url'), true);
  if (xhr.timeout != null) {
    xhr.timeout = Offline.getOption('checks.xhr.timeout');
  }
  checkXHR(xhr, Offline.markUp, Offline.markDown);
  try {
    xhr.send();
  } catch (_error) {
    e = _error;
    Offline.markDown();
  }
  return xhr;
};

picture of the stacktrace

I suspect the catch not really catching everything. What then happens is that some other requests/promises are aborted. That causes other side effects in my application.

Has anyone ever had something similar? Is this done correctly?

Sergeon
  • 6,638
  • 2
  • 23
  • 43
Hendrik
  • 4,849
  • 7
  • 46
  • 51
  • Where are the curly braces? That `try {} catch() {}` doesn't look like the typical javascript exception handling. – ryanyuyu Oct 06 '16 at 20:52
  • @ryanyuyu looks like coffeescript. The tag is missing, though – Bergi Oct 06 '16 at 20:54
  • Ah, not familiar with that preprocessor. Perhaps it should be tagged as such? – ryanyuyu Oct 06 '16 at 20:54
  • Can you show us the whole code? Which error is thrown there? It looks like not `send` is throwing but some other method call, or possibly even an asynchronous one. – Bergi Oct 06 '16 at 20:56
  • 2
    try catch is not going to work with promises/asynchronous calls... Code is done running by the time that error is thrown. – epascarello Oct 06 '16 at 20:57
  • my bad, i added the full code as JS. – Hendrik Oct 06 '16 at 21:03
  • send doesn't actually return anything (i think), you would have to wrap a promise around it and resolve/reject the promise when you get the error back from the `onreadystatechanged` event – Icepickle Oct 06 '16 at 21:06
  • The errors from your screenshot don't seem to have anything to do with the posted code (do they?). What error do you *expect* to catch from the `send` call? – Bergi Oct 06 '16 at 21:11
  • I think that's my problem. I don't really know who is actually causing the error. Apparently like you say they are not from xhr.send directly. – Hendrik Oct 06 '16 at 21:14

0 Answers0