Skip to content
Myr

NetworkError.ts

This file

Path

source/errors/NetworkError.ts

19 lines · 1.1 KiB · TypeScript

Last changed by

Allow customizing error names in TypeScriptSindre Sorhusbefore adoption52b8f93

History of this file

Take it

Add `NetworkError` class and tighten retry logic (#842)Sindre Sorhusbefore adoptioneaf0b80

lines 1–4why, one step back
1import type {KyRequest} from '../types/request.js';
2import {KyError} from './KyError.js';34/**

Readme tweaksSindre Sorhusbefore adoption7f2eade

line 5why, one step back
5Error 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 back
6

Fix a connection dropped while reading the body through shortcuts surfacing as a raw `TypeError`Sindre Sorhusbefore adoption50c3810

line 7why, one step back
7Network 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 back
8
9Note: 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–11why, one step back
10*/
11export class NetworkError extends KyError {

Allow customizing error names in TypeScriptSindre Sorhusbefore adoption52b8f93

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

Add `NetworkError` class and tighten retry logic (#842)Sindre Sorhusbefore adoptioneaf0b80

lines 13–14why, one step back
13 request: KyRequest;
14

Support readonly inputs and explicit undefined in TypeScript optionsSindre Sorhusbefore adoption51b3177

line 15why, one step back
15 constructor(request: Request, options?: {cause?: Error | undefined}) {

Add `NetworkError` class and tighten retry logic (#842)Sindre Sorhusbefore adoptioneaf0b80

lines 16–19why, one step back
16 super(`Request failed due to a network error: ${request.method} ${request.url}`, options);
17 this.request = request;18 }19}