Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Shouldn't the error event of xmlhttprequest have an error message?

I'm working on making an AJAX request from a Firefox extension. I have this code:

function GetMenu(){
   var oReq = Components.classes["@mozilla.org/xmlextras/xmlhttprequest;1"].createInstance();

   // Setup event handlers - must be set before calling open()
   oReq.addEventListener("progress", updateProgress, false);
   oReq.addEventListener("load", transferComplete, false);
   oReq.addEventListener("error", transferFailed, false);
   oReq.addEventListener("abort", transferCanceled, false);

   oReq.open('POST', "http://www.foo.bar/", true);
   oReq.send('your=data&and=more&stuff=here');
}


function transferFailed(evt) {
  Application.console.log("An error occurred while transferring the file.");
  Application.console.log(this.responseText);
  for(var i in evt)     
     Application.console.log(i+ ' => '+evt[i]);
}

The request fails because http://www.foo.bar/ does not exist (I assume). My question is, why is there no error message in the evt object passed to transferFailed() that says, "The domain does not exist" or "DNS failure" or something of that nature? None of the event object's properties have any indication of what the problem is, no message, no error code, etc.

Shouldn't there be some sort of indication of what the actual error is?

like image 827
Nick Avatar asked Sep 04 '13 12:09

Nick


2 Answers

Since you're running with chrome-privileges:

function transferFailed(evt) {
 if (this.channel && this.channel.status == Components.results.NS_ERROR_UNKNOWN_HOST) {
   alert("DNS error");
 }
}

(what @paa said in the comment).

See (you might need to QueryInterface/instanceof accordingly):

  • nsIRequest
  • nsIChannel
  • nsIHttpChannel
  • nsIHttpChannelInternal
like image 55
nmaier Avatar answered Nov 08 '22 10:11

nmaier


Network errors are not propagated to the caller.

status (and statusText, though it's whatever the server likes) is about HTTP.

like image 34
paa Avatar answered Nov 08 '22 11:11

paa