Skip to content
Myr

ForceRetryError.ts

This file

Path

source/errors/ForceRetryError.ts

52 lines · 2.3 KiB · TypeScript

Last changed by

RefactorSindre Sorhusbefore adoptiona5ed57b

History of this file

Take it

Add `ky.retry()` to force retries from `afterResponse` hooks (#779)Sindre Sorhusbefore adoption68a5228

line 1why, one step back
1import type {ForceRetryOptions} from '../core/constants.js';

RefactorSindre Sorhusbefore adoptiona5ed57b

line 2why, one step back
2import {isNonNegativeNumber, isRequest} from '../utils/is.js';

Make `beforeError` hook receive all errors (#829)Sindre Sorhusbefore adoption101c74b

line 3why, one step back
3import {KyError} from './KyError.js';

Add `ky.retry()` to force retries from `afterResponse` hooks (#779)Sindre Sorhusbefore adoption68a5228

lines 4–6why, one step back
4import {NonError} from './NonError.js';
56/**

TweaksSindre Sorhusbefore adoption191057d

lines 7–9why, one step back
7Error 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 back
10*/

Make `beforeError` hook receive all errors (#829)Sindre Sorhusbefore adoption101c74b

line 11why, one step back
11export class ForceRetryError extends KyError {

Allow customizing error names in TypeScriptSindre Sorhusbefore adoption52b8f93

line 12why, one step back
12 override name = 'ForceRetryError';

Add `ky.retry()` to force retries from `afterResponse` hooks (#779)Sindre Sorhusbefore adoption68a5228

lines 13–17why, one step back
13 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 back
18 // `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 }22

Accept a cross-realm `beforeError` error and validate a `ky.retry({delay})`Sindre Sorhusbefore adoptionbd0e26f

line 23why, one step back
23 // 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 back
24 const delay = options?.delay;

RefactorSindre Sorhusbefore adoptiona5ed57b

line 25why, one step back
25 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 back
26 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 back
27 }
28

Report the sent request for a hook's response, and finish the cross-realm and `null` checksSindre Sorhusbefore adoption7f0137d

lines 29–32why, one step back
29 if (options?.code !== undefined && typeof options.code !== 'string') {
30 throw new TypeError('The `ky.retry()` `code` option must be a string');31 }32

RefactorSindre Sorhusbefore adoptiona5ed57b

lines 33–37why, one step back
33 // 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 }37

Add `ky.retry()` to force retries from `afterResponse` hooks (#779)Sindre Sorhusbefore adoption68a5228

lines 38–39why, one step back
38 // Runtime protection: wrap non-Error causes in NonError
39 // TypeScript type is Error for guidance, but JS users can pass anything

RefactorSindre Sorhusbefore adoptiona5ed57b

line 40why, one step back
40 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 back
41
42 super(

Accept a cross-realm `beforeError` error and validate a `ky.retry({delay})`Sindre Sorhusbefore adoptionbd0e26f

lines 43–44why, one step back
43 // `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 back
45 cause ? {cause} : undefined,
46 );4748 this.customDelay = options?.delay;49 this.code = options?.code;50 this.customRequest = options?.request;51 }52}