Comparing version 1.7.0 to 1.8.0
@@ -25,2 +25,3 @@ // tslint:disable | ||
interface Polly { | ||
logger (fn: (err: any) => void): Polly; | ||
handle (fn: (err: any) => boolean): Polly; | ||
@@ -27,0 +28,0 @@ retry (numRetries: number): Retryable; |
{ | ||
"name": "polly-js", | ||
"version": "1.7.0", | ||
"version": "1.8.0", | ||
"description": "Transient exception handling", | ||
@@ -5,0 +5,0 @@ "main": "src/polly.js", |
@@ -32,3 +32,3 @@ # polly-js | ||
## Deciding what to do on failure | ||
Whenever a failure is detected Polly-js will try attempt to retry your action. | ||
Whenever a failure is detected Polly-js will attempt to retry your action. | ||
@@ -45,3 +45,2 @@ You get to control how a failure is retried by calling either ```polly().retry()``` or ```polly().waitAndRetry()```. | ||
## Usage | ||
@@ -118,4 +117,22 @@ | ||
## Logging errors | ||
You can set a logger function to be called every time an error is detected. | ||
```JavaScript | ||
polly() | ||
.logger(function(err){ | ||
console.error(err); //will be hit 2 times | ||
}) | ||
.retry(2) | ||
.executeForPromise(function () { | ||
return requestPromise('http://foo.bar.com'); | ||
}) | ||
.then(function(result) { | ||
//do some important stuff here | ||
}, function(err) { | ||
console.error('Failed trying three times', err);//third error is passed back to the caller | ||
}); | ||
``` | ||
## Acknowledgements | ||
The library is based on the [Polly NuGet package](https://www.nuget.org/packages/Polly/) by Michael Wolfenden |
110
src/polly.js
@@ -6,6 +6,6 @@ /** | ||
(function (root, factory) { | ||
if (typeof define === 'function' && define.amd) { | ||
if (typeof define === "function" && define.amd) { | ||
// AMD. Register as an anonymous module. | ||
define([], factory); | ||
} else if (typeof exports === 'object') { | ||
} else if (typeof exports === "object") { | ||
// Node. Does not work with strict CommonJS, but | ||
@@ -19,7 +19,7 @@ // only CommonJS-like environments that support module.exports, | ||
} | ||
}(this, function () { | ||
'use strict'; | ||
})(this, function () { | ||
"use strict"; | ||
var defaults = { | ||
delay: 100 | ||
delay: 100, | ||
}; | ||
@@ -32,6 +32,6 @@ | ||
try { | ||
return cb({count: count}); | ||
} | ||
catch (ex) { | ||
return cb({ count: count }); | ||
} catch (ex) { | ||
if (count < config.count && config.handleFn(ex)) { | ||
config.loggerFn(ex); | ||
count++; | ||
@@ -50,14 +50,18 @@ } else { | ||
function execute() { | ||
var original = cb({count: count}); | ||
var original = cb({ count: count }); | ||
original.then(function (e) { | ||
resolve(e); | ||
}, function (e) { | ||
if (count < config.count && config.handleFn(e)) { | ||
count++; | ||
execute(); | ||
} else { | ||
reject(e); | ||
original.then( | ||
function (e) { | ||
resolve(e); | ||
}, | ||
function (e) { | ||
if (count < config.count && config.handleFn(e)) { | ||
config.loggerFn(e); | ||
count++; | ||
execute(); | ||
} else { | ||
reject(e); | ||
} | ||
} | ||
}) | ||
); | ||
} | ||
@@ -74,16 +78,20 @@ | ||
function execute() { | ||
var original = cb({count: count}); | ||
var original = cb({ count: count }); | ||
original.then(function (e) { | ||
resolve(e); | ||
}, function (e) { | ||
var delay = config.delays.shift(); | ||
original.then( | ||
function (e) { | ||
resolve(e); | ||
}, | ||
function (e) { | ||
var delay = config.delays.shift(); | ||
if (delay && config.handleFn(e)) { | ||
count++; | ||
setTimeout(execute, delay); | ||
} else { | ||
reject(e); | ||
if (delay && config.handleFn(e)) { | ||
config.loggerFn(e); | ||
count++; | ||
setTimeout(execute, delay); | ||
} else { | ||
reject(e); | ||
} | ||
} | ||
}) | ||
); | ||
} | ||
@@ -95,3 +103,2 @@ | ||
function executeForNode(config, fn, callback) { | ||
@@ -102,11 +109,11 @@ var count = 0; | ||
if (err && count < config.count && config.handleFn(err)) { | ||
config.loggerFn(err); | ||
count++; | ||
fn(internalCallback, {count: count}); | ||
fn(internalCallback, { count: count }); | ||
} else { | ||
callback(err, data); | ||
} | ||
} | ||
fn(internalCallback, {count: count}); | ||
fn(internalCallback, { count: count }); | ||
} | ||
@@ -120,5 +127,6 @@ | ||
if (err && delay && config.handleFn(err)) { | ||
config.loggerFn(err); | ||
count++; | ||
setTimeout(function () { | ||
fn(internalCallback, {count: count}); | ||
fn(internalCallback, { count: count }); | ||
}, delay); | ||
@@ -130,7 +138,8 @@ } else { | ||
fn(internalCallback, {count: count}); | ||
fn(internalCallback, { count: count }); | ||
} | ||
function delayCountToDelays(count) { | ||
var delays = [], delay = defaults.delay; | ||
var delays = [], | ||
delay = defaults.delay; | ||
@@ -151,8 +160,16 @@ for (var i = 0; i < count; i++) { | ||
return true; | ||
} | ||
}, | ||
loggerFn: function (err) {}, | ||
}; | ||
return { | ||
logger: function (loggerFn) { | ||
if (typeof loggerFn === "function") { | ||
config.loggerFn = loggerFn; | ||
} | ||
return this; | ||
}, | ||
handle: function (handleFn) { | ||
if (typeof handleFn === 'function') { | ||
if (typeof handleFn === "function") { | ||
config.handleFn = handleFn; | ||
@@ -164,3 +181,3 @@ } | ||
retry: function (count) { | ||
if (typeof count === 'number') { | ||
if (typeof count === "number") { | ||
config.count = count; | ||
@@ -172,7 +189,7 @@ } | ||
executeForPromise: executeForPromise.bind(null, config), | ||
executeForNode: executeForNode.bind(null, config) | ||
executeForNode: executeForNode.bind(null, config), | ||
}; | ||
}, | ||
waitAndRetry: function (delays) { | ||
if (typeof delays === 'number') { | ||
if (typeof delays === "number") { | ||
delays = delayCountToDelays(delays); | ||
@@ -186,6 +203,9 @@ } | ||
return { | ||
executeForPromise: executeForPromiseWithDelay.bind(null, config), | ||
executeForNode: executeForNodeWithDelay.bind(null, config) | ||
executeForPromise: executeForPromiseWithDelay.bind( | ||
null, | ||
config | ||
), | ||
executeForNode: executeForNodeWithDelay.bind(null, config), | ||
}; | ||
} | ||
}, | ||
}; | ||
@@ -196,2 +216,2 @@ }; | ||
return pollyFn; | ||
})); | ||
}); |
@@ -1,6 +0,6 @@ | ||
'use strict'; | ||
"use strict"; | ||
var chai = require('chai'); | ||
var chaiAsPromised = require('chai-as-promised'); | ||
var requestPromise = require('request-promise'); | ||
var chai = require("chai"); | ||
var chaiAsPromised = require("chai-as-promised"); | ||
var requestPromise = require("request-promise"); | ||
@@ -10,7 +10,6 @@ chai.use(chaiAsPromised); | ||
var polly = require('..'); | ||
var polly = require(".."); | ||
describe('The retry policy with a asynchronous promise call', function () { | ||
it('should return the result when no error', function () { | ||
describe("The retry policy with a asynchronous promise call", function () { | ||
it("should return the result when no error", function () { | ||
return polly() | ||
@@ -24,4 +23,3 @@ .retry() | ||
it('should reject after an error', function () { | ||
it("should reject after an error", function () { | ||
return polly() | ||
@@ -32,7 +30,6 @@ .retry() | ||
}) | ||
.should.eventually | ||
.be.rejectedWith(Error, 'Wrong value'); | ||
.should.eventually.be.rejectedWith(Error, "Wrong value"); | ||
}); | ||
it('should retry once after an error and still fail', function () { | ||
it("should retry once after an error and still fail", function () { | ||
var actualRetryCount = 0; | ||
@@ -42,3 +39,3 @@ | ||
.retry() | ||
.executeForPromise(function ({count}) { | ||
.executeForPromise(function ({ count }) { | ||
return new Promise(function (resolve, reject) { | ||
@@ -49,5 +46,3 @@ actualRetryCount = count; | ||
}) | ||
.should.eventually | ||
.be.rejected | ||
.then(function () { | ||
.should.eventually.be.rejected.then(function () { | ||
actualRetryCount.should.equal(1); | ||
@@ -57,3 +52,3 @@ }); | ||
it('should retry five times after an error and still fail', function () { | ||
it("should retry five times after an error and still fail", function () { | ||
var actualRetryCount = 0; | ||
@@ -63,3 +58,3 @@ | ||
.retry(5) | ||
.executeForPromise(function ({count}) { | ||
.executeForPromise(function ({ count }) { | ||
return new Promise(function (resolve, reject) { | ||
@@ -70,5 +65,3 @@ actualRetryCount = count; | ||
}) | ||
.should.eventually | ||
.be.rejected | ||
.then(function () { | ||
.should.eventually.be.rejected.then(function () { | ||
actualRetryCount.should.equal(5); | ||
@@ -78,3 +71,3 @@ }); | ||
it('should retry once after an error and succeed', function () { | ||
it("should retry once after an error and succeed", function () { | ||
var actualRetryCount = 0; | ||
@@ -84,3 +77,3 @@ | ||
.retry() | ||
.executeForPromise(function ({count}) { | ||
.executeForPromise(function ({ count }) { | ||
return new Promise(function (resolve, reject) { | ||
@@ -101,3 +94,3 @@ actualRetryCount = count; | ||
it('should retry four times after an error and succeed', function () { | ||
it("should retry four times after an error and succeed", function () { | ||
var actualRetryCount = 0; | ||
@@ -107,3 +100,3 @@ | ||
.retry(5) | ||
.executeForPromise(function ({count}) { | ||
.executeForPromise(function ({ count }) { | ||
return new Promise(function (resolve, reject) { | ||
@@ -124,3 +117,3 @@ actualRetryCount = count; | ||
it('we can load html from Google', function () { | ||
it("we can load html from Google", function () { | ||
var actualRetryCount = 0; | ||
@@ -130,13 +123,12 @@ | ||
.retry() | ||
.executeForPromise(function ({count}) { | ||
.executeForPromise(function ({ count }) { | ||
actualRetryCount = count; | ||
return requestPromise('http://www.google.com'); | ||
return requestPromise("http://www.google.com"); | ||
}) | ||
.should.eventually.be.fulfilled | ||
.then(function () { | ||
.should.eventually.be.fulfilled.then(function () { | ||
actualRetryCount.should.equal(0); | ||
}) | ||
}); | ||
}); | ||
it('we can\'t load html from an invalid URL', function () { | ||
it("we can't load html from an invalid URL", function () { | ||
var actualRetryCount = 0; | ||
@@ -146,13 +138,12 @@ | ||
.retry() | ||
.executeForPromise(function ({count}) { | ||
.executeForPromise(function ({ count }) { | ||
actualRetryCount = count; | ||
return requestPromise('http://www.this-is-no-site.com'); | ||
return requestPromise("http://www.this-is-no-site.com"); | ||
}) | ||
.should.eventually.be.rejected | ||
.then(function () { | ||
.should.eventually.be.rejected.then(function () { | ||
actualRetryCount.should.equal(1); | ||
}) | ||
}); | ||
}); | ||
it('should retry five times if handling the error after an error and still fail', function () { | ||
it("should retry five times if handling the error after an error and still fail", function () { | ||
var actualRetryCount = 0; | ||
@@ -165,3 +156,3 @@ | ||
.retry(5) | ||
.executeForPromise(function ({count}) { | ||
.executeForPromise(function ({ count }) { | ||
return new Promise(function (resolve, reject) { | ||
@@ -172,5 +163,3 @@ actualRetryCount = count; | ||
}) | ||
.should.eventually | ||
.be.rejected | ||
.then(function () { | ||
.should.eventually.be.rejected.then(function () { | ||
actualRetryCount.should.equal(5); | ||
@@ -180,3 +169,3 @@ }); | ||
it('should not retry if not handling the error and still fail', function () { | ||
it("should not retry if not handling the error and still fail", function () { | ||
var actualRetryCount = 0; | ||
@@ -189,3 +178,3 @@ | ||
.retry(5) | ||
.executeForPromise(function ({count}) { | ||
.executeForPromise(function ({ count }) { | ||
return new Promise(function (resolve, reject) { | ||
@@ -196,5 +185,3 @@ actualRetryCount = count; | ||
}) | ||
.should.eventually | ||
.be.rejected | ||
.then(function () { | ||
.should.eventually.be.rejected.then(function () { | ||
actualRetryCount.should.equal(0); | ||
@@ -201,0 +188,0 @@ }); |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
253906
969
136