Skip to content

Commit

Permalink
Merge pull request #1 from batmat/option-to-disable-logging
Browse files Browse the repository at this point in the history
Add an options.verbose_logging option to enable/disable logging
  • Loading branch information
void666 committed Jul 17, 2018
2 parents 9b6b35c + 607f77f commit 95169f9
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 5 deletions.
18 changes: 13 additions & 5 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,17 @@ const logger = require('./modules/logger')('request-promise-retry');

class rpRetry {
static _rpRetry(options) {
logger.info(`calling ${options.uri} with retry ${options.retry}`);
if(options.verbose_logging) {
logger.info(`calling ${options.uri} with retry ${options.retry}`);
}
const tries = options.retry || 1;
delete options.retry;
const fetchDataWithRetry = tryCount => {
return requestPromise(options)
.then(result => {
logger.info(`Result obtained for ${options.method} request to ${options.uri}`);
if(options.verbose_logging) {
logger.info(`Result obtained for ${options.method} request to ${options.uri}`);
}
return Promise.resolve(result);
})
.catch(err => {
Expand All @@ -27,10 +31,14 @@ class rpRetry {
}

static _rp(options) {
logger.info(`calling ${options.uri} without retries`);
if(options.verbose_logging) {
logger.info(`calling ${options.uri} without retries`);
}
return requestPromise(options)
.then(result => {
logger.info(`Result obtained for ${options.method} request to ${options.uri}`);
if(options.verbose_logging) {
logger.info(`Result obtained for ${options.method} request to ${options.uri}`);
}
return Promise.resolve(result);
})
.catch(err => {
Expand All @@ -57,4 +65,4 @@ class rpRetry {
}
}

module.exports = rpRetry.rp;
module.exports = rpRetry.rp;
23 changes: 23 additions & 0 deletions test/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,22 @@ const optionsWithRetry = {
method: 'GET',
retry: 4
};
const optionsWithRetryAndLogging = {
uri: 'https://developer.github.com/v3/activity/events/#list-public-events-performed-by-a-user',
method: 'GET',
verbose_logging: true,
retry: 4
};

const optionsWithoutRetry = {
uri: 'https://developer.github.com/v3/activity/events/#list-public-events-performed-by-a-user',
method: 'GET'
};
const optionsWithoutRetryWithLogging = {
uri: 'https://developer.github.com/v3/activity/events/#list-public-events-performed-by-a-user',
method: 'GET',
verbose_logging: true
};
const optionsBadRetry1 = {
uri: 'https://developer.github.com/v3/activity/events/#list-public-events-performed-by-a-user',
method: 'GET',
Expand All @@ -43,6 +54,12 @@ describe('request-promise-retry', function () {
expect(data.error).equal(undefined);
});
});
it('should pass, with retry options, with verbose logging', () => {
return rp(optionsWithRetryAndLogging)
.then(data => {
expect(data.error).equal(undefined);
});
});
it('fail and retry 3 times', () => {
return rp(optionsWithRetryFail)
.catch(err => {
Expand All @@ -55,6 +72,12 @@ describe('request-promise-retry', function () {
expect(data.error).equal(undefined);
});
});
it('should pass, without retry options, with logging', () => {
return rp(optionsWithoutRetryWithLogging)
.then(data => {
expect(data.error).equal(undefined);
});
});
it('should fail, without retry options', () => {
return rp(optionsWithoutRetryFail)
.catch(err => {
Expand Down

0 comments on commit 95169f9

Please sign in to comment.