Skip to content

Commit

Permalink
Remove unused error and response modification methods
Browse files Browse the repository at this point in the history
  • Loading branch information
Michael Ridgway committed Dec 22, 2014
1 parent 5b3ed66 commit 8ca5dbb
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 35 deletions.
2 changes: 1 addition & 1 deletion examples/simple/shared/getFlickrPhotos.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ module.exports = function flickrRead (fetcher, callback) {
},
function(err, data) {
if (err) {
callback && callback(new Error('failed to fetch data' + err));
callback && callback(new Error('failed to fetch data ' + err.message));
}
callback && callback(null, data);
});
Expand Down
43 changes: 9 additions & 34 deletions libs/util/http.client.js
Original file line number Diff line number Diff line change
Expand Up @@ -61,41 +61,14 @@ function shouldRetry(method, config, statusCode) {
if (!isIdempotent && !config.unsafeAllowRetry) {
return false;
}
if ((statusCode !== 408 && statusCode !== 999) || config.tmp.retry_counter >= config.retry.max_retries) {
if ((statusCode !== 0 && statusCode !== 408 && statusCode !== 999) || config.tmp.retry_counter >= config.retry.max_retries) {
return false;
}
config.tmp.retry_counter++;
config.retry.interval = config.retry.interval * 2;
return true;
}

function processErrorResponse(response) {
// Based on YUI doc, response is supposed to be an object, containing at least status and statusText
// In case it is not, log the error as status code 999, and which will be a special status code
// that allows retry (other than 408)
response = response || {status: 999, statusText: 'null response'};
if (response.status === 0 && response.statusText === TIMEOUT) {
// client side JS canceled the XHR
response.status = 408;
response.timeoutBy = 'client';
}
return response;
}

function createErrorObj(response, timeout) {
// caller already makes sure response is not undefined
var errObj = {
statusCode: response.status,
statusText: response.statusText,
responseText: response.responseText
};
if (errObj.statusCode === 408) {
errObj.timeoutBy = response.timeoutBy || '';
errObj.timeout = timeout || '';
}
return errObj;
}

function mergeConfig(config) {
var cfg = {
timeout: DEFAULT_CONFIG.timeout,
Expand Down Expand Up @@ -150,13 +123,12 @@ function doXhr(method, url, headers, data, config, callback) {
timeout : timeout,
headers: headers,
on : {
success : function (id, response) {
success : function (err, response) {
callback(NULL, response);
},
failure : function (id, response) {
response = processErrorResponse(response);
failure : function (err, response) {
if (!shouldRetry(method, config, response.status)) {
callback(createErrorObj(response, timeout));
callback(err);
} else {
_.delay(
function retryXHR() { xhr(method, url, headers, data, config, callback); },
Expand Down Expand Up @@ -192,14 +164,17 @@ function io(url, options) {

err = new Error(errMessage);
err.statusCode = status;
if (408 === status || 0 === status) {
err.timeout = options.timeout;
}
}

resp.responseText = body;

if (err) {
options.on.failure.call(resp, err, resp);
options.on.failure.call(null, err, resp);
} else {
options.on.success.call(resp, null, resp);
options.on.success.call(null, null, resp);
}
});
}
Expand Down

0 comments on commit 8ca5dbb

Please sign in to comment.