Skip to content

Commit dc209a6

Browse files
committed
Refactor: Apply v2.0.0 retry changes to v1.
1 parent a766b68 commit dc209a6

4 files changed

Lines changed: 39 additions & 5 deletions

File tree

lib/client.js

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -339,20 +339,19 @@ function toRetry(err) {
339339

340340
function retry(fn, ctx) {
341341
ctx.res.retries = [];
342-
ctx.res.maxAttempts = ctx.retries;
343-
const request = fn.bind(this, ctx);
342+
const maxAttempts = ctx.retries;
344343

345344
function attempt(i) {
346-
return request()
345+
return fn(ctx)
347346
.catch((err) => {
348-
if (ctx.retries > 0) {
347+
if (maxAttempts > 0) {
349348
const delayBy = rejectedPromise(ctx.retryDelay);
350349
return delayBy(err);
351350
}
352351
throw err;
353352
})
354353
.catch((err) => {
355-
if (i < ctx.retries && isCriticalError(err)) {
354+
if (i < maxAttempts && isCriticalError(err)) {
356355
ctx.res.retries.push(toRetry(err));
357356
return attempt(++i);
358357
}

lib/context.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ const USER_AGENT = `${packageInfo.name}/${packageInfo.version}`;
1212

1313
class Context {
1414
constructor(defaults) {
15+
this.retries = 0;
1516
this.plugins = [];
1617
this.req = Request.create();
1718
this.res = Response.create();

lib/response.js

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
'use strict';
22

3+
const _ = require('lodash');
4+
35
const REQUIRED_PROPERTIES = [
46
'url',
57
'body',
@@ -15,6 +17,7 @@ class Response {
1517
this.url;
1618
this.statusCode;
1719
this.body;
20+
this._retries;
1821
}
1922

2023
getHeader(header) {
@@ -42,6 +45,18 @@ class Response {
4245
return JSON.stringify(this.body).length;
4346
}
4447

48+
get retries() {
49+
if (_.isUndefined(this._retries)) return [];
50+
return this._retries;
51+
}
52+
53+
set retries(retries) {
54+
if (!Array.isArray(retries)) {
55+
retries = [];
56+
}
57+
this._retries = retries;
58+
}
59+
4560
toJSON() {
4661
return {
4762
body: this.body,

test/response.js

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,4 +100,23 @@ describe('Response', () => {
100100
const asJson = JSON.parse(asString);
101101
assert.deepEqual(asJson, state);
102102
});
103+
104+
it('defaults retries to an empty array', () => {
105+
const response = Response.create();
106+
assert.deepEqual(response.retries, []);
107+
});
108+
109+
it('returns an array of retries', () => {
110+
const retries = [{ a: 1 }, { b: 2 }];
111+
const response = Response.create();
112+
response.retries = retries;
113+
assert.deepEqual(response.retries, retries);
114+
});
115+
116+
it('always sets retries to an empty array', () => {
117+
const retries = [];
118+
const response = Response.create();
119+
response.retries = 'crazy input';
120+
assert.deepEqual(response.retries, retries);
121+
});
103122
});

0 commit comments

Comments
 (0)