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.
The
onStateChangeparameter indicating whether there was a connection error isaStatus. For example you could useComponents.isSuccessCode:You could also compare
aStatuswithComponents.results.NS_ERROR_UNKNOWN_HOSTwhich 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.