requestretry
Advanced tools
Comparing version 3.0.2 to 3.1.0
@@ -0,1 +1,11 @@ | ||
<a name="3.0.2"></a> | ||
## <small>3.0.2 (2018-10-23)</small> | ||
* Release v3.0.2. ([83be067](https://github.com/FGRibreau/node-request-retry/commit/83be067)) | ||
* Update README.md ([f8d0972](https://github.com/FGRibreau/node-request-retry/commit/f8d0972)) | ||
* fix: upgrade request peerDep. ([0f9d33c](https://github.com/FGRibreau/node-request-retry/commit/0f9d33c)) | ||
* docs(changelog): updated ([e77598c](https://github.com/FGRibreau/node-request-retry/commit/e77598c)) | ||
<a name="3.0.1"></a> | ||
@@ -2,0 +12,0 @@ ## <small>3.0.1 (2018-09-26)</small> |
12
index.js
@@ -83,3 +83,3 @@ 'use strict'; | ||
* Return true if the request should be retried | ||
* @type {Function} (err, response) -> Boolean | ||
* @type {Function} (err, response, body, options) -> [Boolean, Object (optional)] | ||
*/ | ||
@@ -134,3 +134,11 @@ this.retryStrategy = _.isFunction(options.retryStrategy) ? options.retryStrategy : RetryStrategies.HTTPOrNetworkError; | ||
if (this.retryStrategy(err, response, body) && this.maxAttempts > 0) { | ||
var mustRetry = this.retryStrategy(err, response, body, _.cloneDeep(this.options)); | ||
if (_.isObject(mustRetry)) { | ||
if (_.isObject(mustRetry.options)) { | ||
this.options = mustRetry.options; //if retryStrategy supposes different request options for retry | ||
} | ||
mustRetry = mustRetry.mustRetry; | ||
} | ||
if (mustRetry && this.maxAttempts > 0) { | ||
this._timeout = setTimeout(this._tryUntilFail.bind(this), this.delayStrategy.call(this, err, response, body)); | ||
@@ -137,0 +145,0 @@ return; |
{ | ||
"name": "requestretry", | ||
"description": "request-retry wrap nodejs request to retry http(s) requests in case of error", | ||
"version": "3.0.2", | ||
"version": "3.1.0", | ||
"author": { | ||
@@ -55,16 +55,16 @@ "name": "Francois-Guillaume Ribreau", | ||
"bluebird": "^3.5.1", | ||
"chai": "^4.1.2", | ||
"chai": "^4.2.0", | ||
"conventional-changelog": "^2.0.1", | ||
"conventional-changelog-cli": "^1.3.2", | ||
"conventional-changelog-cli": "^2.0.11", | ||
"coveralls": "^2.13.1", | ||
"kew": "~0.7.0", | ||
"mocha": "^3.5.0", | ||
"mocha": "^5.2.0", | ||
"npm-release": "^1.0.0", | ||
"nyc": "^12.0.2", | ||
"q": "^1.5.1", | ||
"request": "^2.87.0", | ||
"rsvp": "^3.6.2", | ||
"request": "^2.88.0", | ||
"rsvp": "^4.8.4", | ||
"sinon": "^6.1.4", | ||
"updtr": "^2.0.0" | ||
"updtr": "^3.1.0" | ||
} | ||
} |
@@ -116,5 +116,6 @@ <div align="center"> | ||
* @param {Object} body | ||
* @param {Object} options copy | ||
* @return {Boolean} true if the request should be retried | ||
*/ | ||
function myRetryStrategy(err, response, body){ | ||
function myRetryStrategy(err, response, body, options){ | ||
// retry the request if we had an error or if the response was a 'Bad Gateway' | ||
@@ -124,2 +125,18 @@ return err || response.statusCode === 502; | ||
/** | ||
* @param {Null | Object} err | ||
* @param {Object} response | ||
* @param {Object} body | ||
* @param {Object} options copy | ||
* @return {Object} mustRetry: {Boolean} true if the request should be retried | ||
* options: {Object} new options for request | ||
*/ | ||
function myRetryStrategy(err, response, body, options){ | ||
options.url = 'new url'; //you can overwrite some attributes or create new object | ||
return { | ||
mustRetry: err || response.statusCode === 502, | ||
options: options, //then it should be passed back, it will be used for new requests | ||
} | ||
} | ||
request({ | ||
@@ -126,0 +143,0 @@ url: 'https://api.domain.com/v1/a/b' |
@@ -41,2 +41,3 @@ 'use strict'; | ||
it('should call delay strategy 2 times after some retries', function (done) { | ||
this.timeout(3000); | ||
var mockDelayStrategy = sinon.stub().returns(500); | ||
@@ -43,0 +44,0 @@ var startTime = process.hrtime(); |
@@ -7,13 +7,62 @@ 'use strict'; | ||
describe('RetryStrategies', function () { | ||
it('should have a strategy `HTTPError` to only retry on HTTP errors', function () { | ||
checkHTTPErrors(request.RetryStrategies.HTTPError); | ||
}); | ||
it('should have a strategy `NetworkError` to only retry on nodejs network errors', function () { | ||
checkNetworkErrors(request.RetryStrategies.NetworkError, request.RetryStrategies.NetworkError.RETRIABLE_ERRORS); | ||
describe('Default strategies', function () { | ||
it('should have a strategy `HTTPError` to only retry on HTTP errors', function () { | ||
checkHTTPErrors(request.RetryStrategies.HTTPError); | ||
}); | ||
it('should have a strategy `NetworkError` to only retry on nodejs network errors', function () { | ||
checkNetworkErrors(request.RetryStrategies.NetworkError, request.RetryStrategies.NetworkError.RETRIABLE_ERRORS); | ||
}); | ||
it('should have a strategy `HTTPOrNetworkError` to only retry on nodejs network and HTTP errors', function () { | ||
checkHTTPErrors(request.RetryStrategies.HTTPOrNetworkError); | ||
checkNetworkErrors(request.RetryStrategies.HTTPOrNetworkError, request.RetryStrategies.NetworkError.RETRIABLE_ERRORS); | ||
}); | ||
}); | ||
it('should have a strategy `HTTPOrNetworkError` to only retry on nodejs network and HTTP errors', function () { | ||
checkHTTPErrors(request.RetryStrategies.HTTPOrNetworkError); | ||
checkNetworkErrors(request.RetryStrategies.HTTPOrNetworkError, request.RetryStrategies.NetworkError.RETRIABLE_ERRORS); | ||
describe('Custom strategies', function () { | ||
it('should overwrite `options` object if strategy returned it', function (done) { | ||
var strategy = function (err, response, body, options) { | ||
options.url = 'http://www.filltext.com/?rows=1&err=200'; //overwrite url to return 200 | ||
return { | ||
mustRetry: true, | ||
options: options, | ||
}; | ||
}; | ||
request({ | ||
url: 'http://www.filltext.com/?rows=1&err=500', // returns a 500 status | ||
maxAttempts: 3, | ||
retryDelay: 100, | ||
retryStrategy: strategy | ||
}, function(err, response, body) { | ||
if(err) done(err); | ||
t.strictEqual(200, response.statusCode); | ||
done(); | ||
}); | ||
}); | ||
it('should not overwrite `options` object if strategy did not returned it', function (done) { | ||
var strategy = function (err, response, body, options) { | ||
options.url = 'http://www.filltext.com/?rows=1&err=200'; //overwrite url to return 200 | ||
//do not return `options` | ||
return { | ||
mustRetry: true, | ||
}; | ||
}; | ||
request({ | ||
url: 'http://www.filltext.com/?rows=1&err=500', // returns a 500 status | ||
maxAttempts: 3, | ||
retryDelay: 100, | ||
retryStrategy: strategy | ||
}, function(err, response, body) { | ||
if(err) done(err); | ||
t.strictEqual(500, response.statusCode); | ||
done(); | ||
}); | ||
}) | ||
}); | ||
@@ -20,0 +69,0 @@ }); |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
230175
1316
256