@@ -211,33 +211,33 @@ export class Ky {
211
211
protected _calculateRetryDelay ( error : unknown ) {
212
212
this . _retryCount ++ ;
213
213
214
- if ( this . _retryCount <= this . _options . retry . limit && ! ( error instanceof TimeoutError ) ) {
215
- if ( error instanceof HTTPError ) {
216
- if ( ! this . _options . retry . statusCodes . includes ( error . response . status ) ) {
217
- return 0 ;
218
- }
214
+ if ( this . _retryCount > this . _options . retry . limit || error instanceof TimeoutError ) {
215
+ throw error ;
216
+ }
219
217
220
- const retryAfter = error . response . headers . get ( 'Retry-After' ) ;
221
- if ( retryAfter && this . _options . retry . afterStatusCodes . includes ( error . response . status ) ) {
222
- let after = Number ( retryAfter ) * 1000 ;
223
- if ( Number . isNaN ( after ) ) {
224
- after = Date . parse ( retryAfter ) - Date . now ( ) ;
225
- }
218
+ if ( error instanceof HTTPError ) {
219
+ if ( ! this . _options . retry . statusCodes . includes ( error . response . status ) ) {
220
+ throw error ;
221
+ }
226
222
227
- const max = this . _options . retry . maxRetryAfter ?? after ;
228
- return after < max ? after : max ;
223
+ const retryAfter = error . response . headers . get ( 'Retry-After' ) ;
224
+ if ( retryAfter && this . _options . retry . afterStatusCodes . includes ( error . response . status ) ) {
225
+ let after = Number ( retryAfter ) * 1000 ;
226
+ if ( Number . isNaN ( after ) ) {
227
+ after = Date . parse ( retryAfter ) - Date . now ( ) ;
229
228
}
230
229
231
- if ( error . response . status === 413 ) {
232
- return 0 ;
233
- }
230
+ const max = this . _options . retry . maxRetryAfter ?? after ;
231
+ return after < max ? after : max ;
234
232
}
235
233
236
- const retryDelay = this . _options . retry . delay ( this . _retryCount ) ;
237
- return Math . min ( this . _options . retry . backoffLimit , retryDelay ) ;
234
+ if ( error . response . status === 413 ) {
235
+ throw error ;
236
+ }
238
237
}
239
238
240
- return 0 ;
239
+ const retryDelay = this . _options . retry . delay ( this . _retryCount ) ;
240
+ return Math . min ( this . _options . retry . backoffLimit , retryDelay ) ;
241
241
}
242
242
243
243
protected _decorateResponse ( response : Response ) : Response {
@@ -253,28 +253,28 @@ export class Ky {
253
253
return await function_ ( ) ;
254
254
} catch ( error ) {
255
255
const ms = Math . min ( this . _calculateRetryDelay ( error ) , maxSafeTimeout ) ;
256
- if ( ms !== 0 && this . _retryCount > 0 ) {
257
- await delay ( ms , { signal : this . _options . signal } ) ;
256
+ if ( this . _retryCount < 1 ) {
257
+ throw error ;
258
+ }
258
259
259
- for ( const hook of this . _options . hooks . beforeRetry ) {
260
- // eslint-disable-next-line no-await-in-loop
261
- const hookResult = await hook ( {
262
- request : this . request ,
263
- options : ( this . _options as unknown ) as NormalizedOptions ,
264
- error : error as Error ,
265
- retryCount : this . _retryCount ,
266
- } ) ;
267
-
268
- // If `stop` is returned from the hook, the retry process is stopped
269
- if ( hookResult === stop ) {
270
- return ;
271
- }
272
- }
260
+ await delay ( ms , { signal : this . _options . signal } ) ;
273
261
274
- return this . _retry ( function_ ) ;
262
+ for ( const hook of this . _options . hooks . beforeRetry ) {
263
+ // eslint-disable-next-line no-await-in-loop
264
+ const hookResult = await hook ( {
265
+ request : this . request ,
266
+ options : ( this . _options as unknown ) as NormalizedOptions ,
267
+ error : error as Error ,
268
+ retryCount : this . _retryCount ,
269
+ } ) ;
270
+
271
+ // If `stop` is returned from the hook, the retry process is stopped
272
+ if ( hookResult === stop ) {
273
+ return ;
274
+ }
275
275
}
276
276
277
- throw error ;
277
+ return this . _retry ( function_ ) ;
278
278
}
279
279
}
280
280
0 commit comments