0

In a standalone XUL app, I'd like to catch the server not found exception. I've tried by checking state in onStateChange event of the nsIWebProgressListener, but this doesn't seem to work. My onStateChange event implementation is as shown below. I'm making the assumption that if STATE_START or STATE_STOP is not returning a valid value, then there's something wrong with page loading, and displays the error message to the user.

onStateChange: function(aProgress, aRequest, aFlag, aStatus) {
  const STATE_START = Components.interfaces.nsIWebProgressListener.STATE_START;
  const STATE_STOP = Components.interfaces.nsIWebProgressListener.STATE_STOP; 

  if(aFlag & STATE_START) {
      document.getElementById("progressBar").hidden = false;
  }
  if(aFlag & STATE_STOP) {
      setTimeout(function() { document.getElementById("progressBar").hidden = true; }, 2000);     
  }

  if(aFlag & (!STATE_START | !STATE_STOP)) {
    alert("Your connection seems to be down. Please confirm with your system admin.");    
  }
  return 0;

},

Can someone kindly advice me on what I'm doing wrong? Thanks in advance.

okello
  • 601
  • 10
  • 27
  • Could you be a little more specific about what you are doing? If I understand correctly, you are trying to load a URL into a `` element and are concerned about an exception appearing in the Error Console? What does that exception say **exactly**? – Wladimir Palant May 13 '12 at 11:03
  • I'm developing a desktop app that acts as a gateway to an online service. The online service launches in a browser element. I want to be able to tell if there is no Internet access, and display an appropriate message to the user. The is loaded using the `loadURI()` method. I've tried surrounding this in a try catch block as shown: `try { document.getElementById("browser-id").loadURI("http://www.online-service.com"); } catch(e) { alert("Your Internet connection seems to be down. Please consult your network admin."); }`, but the catch block is not being executed. – okello May 13 '12 at 13:10
  • Of course not, DNS resolution is an asynchronous action - you cannot expect getting an exception synchronously there. Please edit your question and add the `nsIWebProgressListener` code you tried, that's the correct approach and you must have made a mistake somewhere. – Wladimir Palant May 13 '12 at 13:22

1 Answers1

2

The onStateChange parameter indicating whether there was a connection error is aStatus. For example you could use Components.isSuccessCode:

if ((aFlag & STATE_STOP) && !Components.isSuccessCode(aStatus))
{
  alert("Your connection seems to be down. Please confirm with your system admin.");
}

You could also compare aStatus with Components.results.NS_ERROR_UNKNOWN_HOST which corresponds to the "Server not found" error. If the connection is down there could be a number of other errors however, e.g. NS_ERROR_CONNECTION_REFUSED (connection failed), NS_ERROR_UNKNOWN_PROXY_HOST (proxy not found), NS_ERROR_OFFLINE (attempt to connect while in offline state). You can find the complete list of network error codes in nsNetError.h.

Wladimir Palant
  • 56,865
  • 12
  • 98
  • 126
  • Thank you Palant. `if ((aFlag & STATE_STOP) && (aStatus == Components.results.NS_ERROR_UNKNOWN_HOST)) { alert("Your connection seems to be down. Please confirm with your network admin."); }` works very well. But the code as stated above throws an exception even when there's a connection. – okello May 14 '12 at 08:22
  • There are different error codes, most of them don't mean that the user is offline - you should check which one you get. – Wladimir Palant May 14 '12 at 08:35
  • Thanks. I've implemented error checking for NS_ERROR_UNKNOWN HOST, NS_ERROR_CONNECTION_REFUSED, NS_ERROR_OFFLINE, and NS_ERROR_UNKNOWN_PROXY. The app includes links to the online service, so the user is not required to enter a URL. Indeed there's no provision for a user to enter a URL. I think this limits the range of errors that the user can encounter. – okello May 14 '12 at 08:45