From 6794142b19808f5bb3af73dda2ace61e334f5014 Mon Sep 17 00:00:00 2001 From: Cleverson Nascimento Date: Tue, 19 Jan 2021 11:55:40 -0300 Subject: [PATCH] Added possibility to log on each error. --- README.md | 21 +++++++++++++++++++-- d.ts/polly-js.d.ts | 1 + src/polly.js | 15 ++++++++++++++- 3 files changed, 34 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index d0c463b..f953437 100644 --- a/README.md +++ b/README.md @@ -29,7 +29,7 @@ When you call ```polly().executeForPromise()``` the code assumes When you call ```polly().executeForNode()``` 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. @@ -41,7 +41,6 @@ With ```polly().waitAndRetry()``` it will double the time between each try. If y Using ```polly().handle()``` 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. @@ -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 diff --git a/d.ts/polly-js.d.ts b/d.ts/polly-js.d.ts index bdf0fed..e1335ae 100644 --- a/d.ts/polly-js.d.ts +++ b/d.ts/polly-js.d.ts @@ -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; diff --git a/src/polly.js b/src/polly.js index 74debe4..d725ba0 100644 --- a/src/polly.js +++ b/src/polly.js @@ -31,6 +31,7 @@ } catch (ex) { if (count < config.count && config.handleFn(ex)) { + config.loggerFn(err); count++; } else { throw ex; @@ -50,6 +51,7 @@ resolve(e); }, function (e) { if (count < config.count && config.handleFn(e)) { + config.loggerFn(err); count++; execute(); } else { @@ -75,6 +77,7 @@ var delay = config.delays.shift(); if (delay && config.handleFn(e)) { + config.loggerFn(err); count++; setTimeout(execute, delay); } else { @@ -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); - } } @@ -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}); @@ -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;