Commit c5ef7ea 1 parent eac4bb7 commit c5ef7ea Copy full SHA for c5ef7ea
File tree 4 files changed +74
-9
lines changed
4 files changed +74
-9
lines changed Original file line number Diff line number Diff line change @@ -144,7 +144,7 @@ async function fetchFileFromNetworkAndWriteToDisk({
144
144
145
145
try {
146
146
await asyncRetry ( {
147
- asyncFn : async ( bail ) => {
147
+ asyncFn : async ( bail , num ) => {
148
148
try {
149
149
await fetchFileFromTargetGatewayAndWriteToDisk ( {
150
150
contentUrl,
@@ -181,9 +181,16 @@ async function fetchFileFromNetworkAndWriteToDisk({
181
181
}
182
182
183
183
// Re-throw any other error to continue with retry logic
184
- throw new Error (
185
- `Failed to fetch content=${ multihash } with statusCode=${ e . response ?. status } . Retrying..`
186
- )
184
+ if ( num === numRetries + 1 ) {
185
+ // Final error thrown
186
+ throw new Error (
187
+ `Failed to fetch content with statusCode=${ e . response ?. status } after ${ num } retries`
188
+ )
189
+ } else {
190
+ throw new Error (
191
+ `Failed to fetch content with statusCode=${ e . response ?. status } . Retrying..`
192
+ )
193
+ }
187
194
}
188
195
} ,
189
196
logger,
Original file line number Diff line number Diff line change @@ -12,7 +12,7 @@ const { logger: genericLogger } = require('../logging')
12
12
* @param {func } param.asyncFn the fn to asynchronously retry
13
13
* @param {Object } param.options optional options. defaults to the params listed below if not explicitly passed in
14
14
* @param {number } [param.options.factor=2] the exponential factor
15
- * @param {number } [param.options.retries=5] the max number of retries. defaulted to 5
15
+ * @param {number } [param.options.retries=5] the max number of retries. defaulted to 5. So, this will attempt once and retry 5 times for a total of 6 tries.
16
16
* @param {number } [param.options.minTimeout=1000] minimum number of ms to wait after first retry. defaulted to 1000ms
17
17
* @param {number } [param.options.maxTimeout=5000] maximum number of ms between two retries. defaulted to 5000ms
18
18
* @param {func } [param.options.onRetry] fn that gets called per retry
Original file line number Diff line number Diff line change @@ -67,7 +67,7 @@ export async function findCIDInNetwork(
67
67
68
68
try {
69
69
found = await asyncRetry ( {
70
- asyncFn : async ( bail : ( e : Error ) => void ) => {
70
+ asyncFn : async ( bail : ( e : Error ) => void , num : number ) => {
71
71
let response
72
72
try {
73
73
response = await axios ( {
@@ -98,9 +98,16 @@ export async function findCIDInNetwork(
98
98
return
99
99
}
100
100
101
- throw new Error (
102
- `Failed to fetch content with statusCode=${ e . response ?. status } . Retrying..`
103
- )
101
+ if ( num === numRetries + 1 ) {
102
+ // Final error thrown
103
+ throw new Error (
104
+ `Failed to fetch content with statusCode=${ e . response ?. status } after ${ num } retries`
105
+ )
106
+ } else {
107
+ throw new Error (
108
+ `Failed to fetch content with statusCode=${ e . response ?. status } . Retrying..`
109
+ )
110
+ }
104
111
}
105
112
106
113
if ( ! response || ! response . data ) {
Original file line number Diff line number Diff line change @@ -16,8 +16,59 @@ describe('test asyncRetry', function () {
16
16
minTimeout : 0 ,
17
17
maxTimeout : 100
18
18
} )
19
+ assert . fail ( 'Should have thrown' )
19
20
} catch ( e ) {
20
21
assert . deepStrictEqual ( e . message , 'Will not retry after 3 retries' )
21
22
}
22
23
} )
24
+
25
+ it ( 'on certain retry, do something' , async function ( ) {
26
+ try {
27
+ let name = ''
28
+ await asyncRetry ( {
29
+ asyncFn : async ( bail , num ) => {
30
+ if ( num === 3 ) {
31
+ name = 'vicky'
32
+ return
33
+ }
34
+
35
+ name = 'not vicky'
36
+ throw new Error ( `Test ${ num } ` )
37
+ } ,
38
+ options : {
39
+ minTimeout : 0 ,
40
+ maxTimeout : 100
41
+ }
42
+ } )
43
+
44
+ assert . deepStrictEqual ( name , 'vicky' )
45
+ } catch ( e ) {
46
+ assert . fail ( 'Should not have thrown' )
47
+ }
48
+ } )
49
+
50
+ it ( 'final error message should be what is specified in num retry check' , async function ( ) {
51
+ try {
52
+ const retries = 3
53
+ await asyncRetry ( {
54
+ asyncFn : async ( bail , num ) => {
55
+ console . log ( 'Test num' , num )
56
+ if ( num === retries + 1 ) {
57
+ throw new Error ( 'vicky' )
58
+ }
59
+
60
+ throw new Error ( `Test ${ num } ` )
61
+ } ,
62
+ options : {
63
+ minTimeout : 0 ,
64
+ maxTimeout : 100 ,
65
+ retries
66
+ }
67
+ } )
68
+
69
+ assert . fail ( 'Should have thrown' )
70
+ } catch ( e ) {
71
+ assert . deepStrictEqual ( e . message , 'vicky' )
72
+ }
73
+ } )
23
74
} )
You can’t perform that action at this time.
0 commit comments