NetworkError.ts
This file
Path
source/errors/NetworkError.ts
19 lines · 1.1 KiB · TypeScript
Last changed by
Support readonly inputs and explicit undefined in TypeScript optionsSindre Sorhusbefore adoption51b3177
History of this fileTake it
Add `NetworkError` class and tighten retry logic (#842)Sindre Sorhusbefore adoptioneaf0b80
lines 1–4why, one step back1import type {KyRequest} from '../types/request.js';2import {KyError} from './KyError.js';34/**Readme tweaksSindre Sorhusbefore adoption7f2eade
line 5why, one step back5Error thrown when a network error occurs during the request (e.g., DNS failure, connection refused, offline). It has a `request` property with the `Request` object. The original error is available via the standard `cause` property.Add `NetworkError` class and tighten retry logic (#842)Sindre Sorhusbefore adoptioneaf0b80
line 6why, one step back6Fix a connection dropped while reading the body through shortcuts surfacing as a raw `TypeError`Sindre Sorhusbefore adoption50c3810
line 7why, one step back7Network errors are automatically retried (for retriable methods). A connection that drops while a Ky shortcut method like `.json()` is reading the response body is also wrapped in `NetworkError`, but it is not retried because the response has already been received.Readme tweaksSindre Sorhusbefore adoption7f2eade
lines 8–9why, one step back89Note: Network errors are detected using runtime-specific heuristics. Unrecognized runtimes may produce errors that are not wrapped in `NetworkError`. Use the `shouldRetry` option to handle such cases.Add `NetworkError` class and tighten retry logic (#842)Sindre Sorhusbefore adoptioneaf0b80
lines 10–14why, one step back10*/11export class NetworkError extends KyError {12 override name = 'NetworkError' as const;13 request: KyRequest;14Support readonly inputs and explicit undefined in TypeScript optionsSindre Sorhusbefore adoption51b3177
line 15why, one step back15 constructor(request: Request, options?: {cause?: Error | undefined}) {Add `NetworkError` class and tighten retry logic (#842)Sindre Sorhusbefore adoptioneaf0b80
lines 16–19why, one step back16 super(`Request failed due to a network error: ${request.method} ${request.url}`, options);17 this.request = request;18 }19}