ForceRetryError.ts
This file
Path
source/errors/ForceRetryError.ts
52 lines · 2.3 KiB · TypeScript
Last changed by
RefactorSindre Sorhusbefore adoptiona5ed57b
History of this fileTake it
Add `ky.retry()` to force retries from `afterResponse` hooks (#779)Sindre Sorhusbefore adoption68a5228
line 1why, one step back1import type {ForceRetryOptions} from '../core/constants.js';RefactorSindre Sorhusbefore adoptiona5ed57b
line 2why, one step back2import {isNonNegativeNumber, isRequest} from '../utils/is.js';Make `beforeError` hook receive all errors (#829)Sindre Sorhusbefore adoption101c74b
line 3why, one step back3import {KyError} from './KyError.js';Add `ky.retry()` to force retries from `afterResponse` hooks (#779)Sindre Sorhusbefore adoption68a5228
lines 4–6why, one step back4import {NonError} from './NonError.js';56/**TweaksSindre Sorhusbefore adoption191057d
lines 7–9why, one step back7Error used to signal a forced retry from `afterResponse` hooks.89This is thrown when `ky.retry()` is returned from an `afterResponse` hook. It is observable in `beforeRetry` and `beforeError` hooks via the `isForceRetryError()` type guard.Add `ky.retry()` to force retries from `afterResponse` hooks (#779)Sindre Sorhusbefore adoption68a5228
line 10why, one step back10*/Make `beforeError` hook receive all errors (#829)Sindre Sorhusbefore adoption101c74b
line 11why, one step back11export class ForceRetryError extends KyError {Allow customizing error names in TypeScriptSindre Sorhusbefore adoption52b8f93
line 12why, one step back12 override name = 'ForceRetryError';Add `ky.retry()` to force retries from `afterResponse` hooks (#779)Sindre Sorhusbefore adoption68a5228
lines 13–17why, one step back13 customDelay: number | undefined;14 code: string | undefined;15 customRequest: Request | undefined;1617 constructor(options?: ForceRetryOptions) {Report the sent request for a hook's response, and finish the cross-realm and `null` checksSindre Sorhusbefore adoption7f0137d
lines 18–22why, one step back18 // `null` is not accepted anywhere in Ky, so it is reported rather than treated like no options.19 if (options === null) {20 throw new TypeError('The `ky.retry()` options must be an object');21 }22Accept a cross-realm `beforeError` error and validate a `ky.retry({delay})`Sindre Sorhusbefore adoptionbd0e26f
line 23why, one step back23 // The custom delay flows straight into `setTimeout`, so a negative or `NaN` value would be clamped to 1ms and silently collapse the backoff, the same failure mode validated away for `retry.delay`.Report the sent request for a hook's response, and finish the cross-realm and `null` checksSindre Sorhusbefore adoption7f0137d
line 24why, one step back24 const delay = options?.delay;RefactorSindre Sorhusbefore adoptiona5ed57b
line 25why, one step back25 if (delay !== undefined && !isNonNegativeNumber(delay)) {Report the sent request for a hook's response, and finish the cross-realm and `null` checksSindre Sorhusbefore adoption7f0137d
line 26why, one step back26 throw new TypeError('The `ky.retry()` `delay` option must be a non-negative number or `Infinity`');Accept a cross-realm `beforeError` error and validate a `ky.retry({delay})`Sindre Sorhusbefore adoptionbd0e26f
lines 27–28why, one step back27 }28Report the sent request for a hook's response, and finish the cross-realm and `null` checksSindre Sorhusbefore adoption7f0137d
lines 29–32why, one step back29 if (options?.code !== undefined && typeof options.code !== 'string') {30 throw new TypeError('The `ky.retry()` `code` option must be a string');31 }32RefactorSindre Sorhusbefore adoptiona5ed57b
lines 33–37why, one step back33 // The request is only read when it is truthy, so `null` would otherwise silently retry with the original request. A `Request` from another realm is accepted, the same way a hook's returned request is.34 if (options?.request !== undefined && !isRequest(options.request)) {35 throw new TypeError('The `ky.retry()` `request` option must be a `Request`');36 }37Add `ky.retry()` to force retries from `afterResponse` hooks (#779)Sindre Sorhusbefore adoption68a5228
lines 38–39why, one step back38 // Runtime protection: wrap non-Error causes in NonError39 // TypeScript type is Error for guidance, but JS users can pass anythingRefactorSindre Sorhusbefore adoptiona5ed57b
line 40why, one step back40 const cause = options?.cause === undefined ? undefined : NonError.wrap(options.cause);Add `ky.retry()` to force retries from `afterResponse` hooks (#779)Sindre Sorhusbefore adoption68a5228
lines 41–42why, one step back4142 super(Accept a cross-realm `beforeError` error and validate a `ky.retry({delay})`Sindre Sorhusbefore adoptionbd0e26f
lines 43–44why, one step back43 // `code` is documented as always reaching the message, so only an absent code falls back.44 options?.code === undefined ? 'Forced retry' : `Forced retry: ${options.code}`,Add `ky.retry()` to force retries from `afterResponse` hooks (#779)Sindre Sorhusbefore adoption68a5228
lines 45–52why, one step back45 cause ? {cause} : undefined,46 );4748 this.customDelay = options?.delay;49 this.code = options?.code;50 this.customRequest = options?.request;51 }52}