Skip to content

Commit

Permalink
Added possibility to log on each error.
Browse files Browse the repository at this point in the history
  • Loading branch information
Cleverson Nascimento committed Jan 19, 2021
1 parent f9932a8 commit 6794142
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 3 deletions.
21 changes: 19 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ When you call ```polly().executeForPromise(<your function>)``` the code assumes
When you call ```polly().executeForNode(<your function that accepts a callback>)``` the code assumes that your code failed and needs to be retried when your function calls the callback with a first non null parameter indicating an error.

## 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.

You get to control how a failure is retried by calling either ```polly().retry()``` or ```polly().waitAndRetry()```.
Both will retry the failing action but the ```polly().waitAndRetry()``` option adds a small delay before retrying.
Expand All @@ -41,7 +41,6 @@ With ```polly().waitAndRetry()``` it will double the time between each try. If y
Using ```polly().handle(<function>)``` you can decide if you want to retry a specific failure. For example you might want to retry an AJAX request returning a 404 response code but not one resulting in a 500 response code.
The callback function specified in ```polly().handle()``` is called with watever error was received so you can use any property from there and you return true if you want to retry the action or false to stop retrying.


## Usage

Try to load the Google home page and retry twice if it fails.
Expand Down Expand Up @@ -113,6 +112,24 @@ const loadData = url => {
const movies = await loadData("http://localhost:3000/movies.json");
```

## 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
1 change: 1 addition & 0 deletions d.ts/polly-js.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ declare module 'polly-js' {
}

interface Polly {
logger (fn: (err: any) => void): Polly;
handle (fn: (err: any) => boolean): Polly;
retry (numRetries: number): Retryable;
waitAndRetry (delays: number[]): AsyncRetryable;
Expand Down
15 changes: 14 additions & 1 deletion src/polly.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
}
catch (ex) {
if (count < config.count && config.handleFn(ex)) {
config.loggerFn(err);
count++;
} else {
throw ex;
Expand All @@ -50,6 +51,7 @@
resolve(e);
}, function (e) {
if (count < config.count && config.handleFn(e)) {
config.loggerFn(err);
count++;
execute();
} else {
Expand All @@ -75,6 +77,7 @@
var delay = config.delays.shift();

if (delay && config.handleFn(e)) {
config.loggerFn(err);
count++;
setTimeout(execute, delay);
} else {
Expand All @@ -93,11 +96,11 @@

function internalCallback(err, data) {
if (err && count < config.count && config.handleFn(err)) {
config.loggerFn(err);
count++;
fn(internalCallback, {count: count});
} else {
callback(err, data);

}
}

Expand All @@ -110,6 +113,7 @@
function internalCallback(err, data) {
var delay = config.delays.shift();
if (err && delay && config.handleFn(err)) {
config.loggerFn(err);
count++;
setTimeout(function () {
fn(internalCallback, {count: count});
Expand Down Expand Up @@ -139,10 +143,19 @@
delays: [defaults.delay],
handleFn: function () {
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') {
config.handleFn = handleFn;
Expand Down

0 comments on commit 6794142

Please sign in to comment.