Fixes #60, make sure it works well offline

- Added detection for network failure -- debug message is output and noNetwork property is added to the xmlhttp object

- Removed onStatus callback from HTTP.doGet and HTTP.doPost -- that was copied over from the Piggy Bank API, but the onDone callback has to handle errors anyway, so it can just check the status code if it actually cares to differentiate non-200 status codes from any other error

- Added error handling for empty responseXML to Schema._updateScrapersRemoteCallback

- Renamed SCHOLAR_CONFIG['REPOSITORY_CHECK_RETRY'] to SCHOLAR_CONFIG['REPOSITORY_RETRY_INTERVAL']
This commit is contained in:
Dan Stillman 2006-06-25 20:14:11 +00:00
parent 6a627fad0a
commit 05c8b0e467
2 changed files with 31 additions and 24 deletions

View File

@ -80,12 +80,12 @@ Scholar.Schema = new function(){
+ 'version=' + Scholar.version;
Scholar.debug('Checking repository for updates (' + url + ')');
var get = Scholar.HTTP.doGet(url, false, _updateScrapersRemoteCallback);
var get = Scholar.HTTP.doGet(url, _updateScrapersRemoteCallback);
// TODO: instead, add an observer to start and stop timer on online state change
if (!get){
Scholar.debug('Browser is offline -- skipping check');
_setRepositoryTimer(SCHOLAR_CONFIG['REPOSITORY_CHECK_RETRY']);
_setRepositoryTimer(SCHOLAR_CONFIG['REPOSITORY_RETRY_INTERVAL']);
}
}
@ -279,7 +279,14 @@ Scholar.Schema = new function(){
* Process the response from the repository
**/
function _updateScrapersRemoteCallback(xmlhttp){
// TODO: error handling
if (!xmlhttp.responseXML){
if (!xmlhttp.noNetwork){
Scholar.debug('Invalid response from repository', 2);
}
_setRepositoryTimer(SCHOLAR_CONFIG['REPOSITORY_RETRY_INTERVAL']);
return false;
}
var currentTime = xmlhttp.responseXML.
getElementsByTagName('currentTime')[0].firstChild.nodeValue;
var updates = xmlhttp.responseXML.getElementsByTagName('scraper');

View File

@ -6,7 +6,7 @@ const SCHOLAR_CONFIG = {
DEBUG_TO_CONSOLE: true, // dump debug messages to console rather than (much slower) Debug Logger
REPOSITORY_URL: 'http://chnm.gmu.edu/firefoxscholar/repo',
REPOSITORY_CHECK_INTERVAL: 3600, // temp -- 86400, // 24 hours
REPOSITORY_CHECK_RETRY: 3600 // 1 hour
REPOSITORY_RETRY_INTERVAL: 3600 // 1 hour
};
/*
@ -466,19 +466,20 @@ Scholar.HTTP = new function(){
*
* Returns false if browser is offline
**/
function doGet(url, onStatus, onDone){
function doGet(url, onDone){
if (this.browserIsOffline()){
return false;
}
var xmlhttp = Components.classes["@mozilla.org/xmlextras/xmlhttprequest;1"]
.createInstance();
.createInstance();
xmlhttp.open('GET', url, true);
var test = xmlhttp.open('GET', url, true);
xmlhttp.onreadystatechange = function(){
_stateChange(xmlhttp, onStatus, onDone);
_stateChange(xmlhttp, onDone);
};
xmlhttp.send(null);
return true;
@ -490,7 +491,7 @@ Scholar.HTTP = new function(){
*
* Returns false if browser is offline
**/
function doPost(url, body, onStatus, onDone){
function doPost(url, body, onDone){
if (this.browserIsOffline()){
return false;
}
@ -501,8 +502,9 @@ Scholar.HTTP = new function(){
xmlhttp.open('POST', url, true);
xmlhttp.onreadystatechange = function(){
_stateChange(xmlhttp, onStatus, onDone);
_stateChange(xmlhttp, onDone);
};
xmlhttp.send(body);
return true;
@ -515,30 +517,28 @@ Scholar.HTTP = new function(){
}
function _stateChange(xmlhttp, onStatus, onDone){
function _stateChange(xmlhttp, onDone){
switch (xmlhttp.readyState){
// Request not yet made
case 1:
break;
// Contact established with server but nothing downloaded yet
case 2:
// Accessing status will throw an exception if no network connection
try {
// Check for HTTP status 200
if (xmlhttp.status != 200){
if (onStatus) {
onStatus(
xmlhttp.status,
xmlhttp.statusText,
xmlhttp
);
xmlhttp.abort();
}
}
xmlhttp.status;
}
catch (e){
Scholar.debug(e, 2);
Scholar.debug('No network connection');
xmlhttp.noNetwork = true;
return false;
}
// Check for HTTP status 200
if (xmlhttp.status != 200){
Scholar.debug('XMLHTTPRequest received HTTP response code '
+ xmlhttp.status);
}
break;