diff --git a/.travis.yml b/.travis.yml new file mode 100644 index 0000000..fa057c3 --- /dev/null +++ b/.travis.yml @@ -0,0 +1,9 @@ +language: node_js +node_js: + - "4" + - "5" + - "6" + - "7" + - "8" + - "9" +sudo: false diff --git a/README.md b/README.md index 1e3ede8..c67a4b4 100644 --- a/README.md +++ b/README.md @@ -47,8 +47,8 @@ Usage: mws.Orders.ListOrders({ MarketplaceId: 'lel', MaxResultsPerPage: 10, - CreatedAfter: new Date(1,1,2015), - CreatedBefore: new Date(1,2,2015) + CreatedAfter: new Date(2015, 1, 1), + CreatedBefore: new Date(2015, 1, 2) }) .then(({ result, metadata }) => { // result diff --git a/lib/client.js b/lib/client.js index 5f53749..d4180a7 100644 --- a/lib/client.js +++ b/lib/client.js @@ -123,6 +123,25 @@ class AmazonMwsClient { * @return Promise */ call(req, q, meta) { + + const doRetry = (err) => { + if (!meta.retry) { + throw err; + } + + const attempt = meta.attempt + 1; + + if (attempt > meta.max_attempts) { + throw err; + } + + // simple exponential backoff for dealing with throttling + const backoffDuration = Math.min(100 * Math.pow(2, attempt), meta.max_backoff); + + return Promise.delay(backoffDuration) + .then(() => this.call(req, q, _.assign({}, meta, { attempt }))); + }; + return this.request(req, q, meta).then((response) => { const body = response.body; @@ -166,40 +185,9 @@ class AmazonMwsClient { metadata: _.concat(metadata, nextData.metadata) })); }) - .catch({ code: 'RequestThrottled' }, (err) => { - if (!meta.retry) { - throw err; - } - - const attempt = meta.attempt + 1; - - if (attempt > meta.max_attempts) { - throw err; - } - - // simple exponential backoff for dealing with throttling - const backoffDuration = Math.min(100 * Math.pow(2, attempt), meta.max_backoff); - - return Promise.delay(backoffDuration) - .then(() => this.call(req, q, _.assign({}, meta, { attempt }))); - }) - .catch({ code: 'QuotaExceeded' }, (err) => { - if (!meta.retry) { - throw err; - } - - const attempt = meta.attempt + 1; - - if (attempt > meta.max_attempts) { - throw err; - } - - // simple exponential backoff for dealing with throttling - const backoffDuration = Math.min(100 * Math.pow(2, attempt), meta.max_backoff); - - return Promise.delay(backoffDuration) - .then(() => this.call(req, q, _.assign({}, meta, { attempt }))); - }); + .catch({ code: 'InternalError' }, doRetry) + .catch({ code: 'RequestThrottled' }, doRetry) + .catch({ code: 'QuotaExceeded' }, doRetry); }); }