Skip to content
Myr

NetworkError.ts

This file

Path

source/errors/NetworkError.ts

19 lines · 891 bytes · TypeScript

Last changed by

Readme tweaksSindre Sorhusbefore adoption7f2eade

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

Readme tweaksSindre Sorhusbefore adoption7f2eade

lines 7–9why, one step back
7Network errors are automatically retried (for retriable methods).
89Note: 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–19why, one step back
10*/
11export class NetworkError extends KyError {12 override name = 'NetworkError' as const;13 request: KyRequest;1415 constructor(request: Request, options?: {cause?: Error}) {16 super(`Request failed due to a network error: ${request.method} ${request.url}`, options);17 this.request = request;18 }19}