Skip to content
Myr

network-error.ts

This file

Path

test/network-error.ts

69 lines · 2.0 KiB · TypeScript

Last changed by

Detect request stream support in Bun and Deno, and cover the pipeline with testsSindre Sorhusbefore adoption83c711c

History of this file

Take it

Support readonly inputs and explicit undefined in TypeScript optionsSindre Sorhusbefore adoption51b3177

line 1why, one step back
1import test from 'ava';

Release the bodies a caller cannot reach and stop sharing search parametersSindre Sorhusbefore adoptionec3d17f

lines 2–3why, one step back
2import ky, {NetworkError, isNetworkError} from '../source/index.js';
3

Detect request stream support in Bun and Deno, and cover the pipeline with testsSindre Sorhusbefore adoption83c711c

lines 4–14why, one step back
4test('NetworkError refers to the failed outgoing request', async t => {
5 const requests: Request[] = [];6 const cause = new TypeError('fetch failed');7 const error = await t.throwsAsync(ky('https://example.com', {8 retry: {limit: 1, delay: () => 0},9 async fetch(request) {10 requests.push(request);11 request.headers.set('x-attempt', String(requests.length));12 throw cause;13 },14 }), {instanceOf: NetworkError});

Release the bodies a caller cannot reach and stop sharing search parametersSindre Sorhusbefore adoptionec3d17f

line 15why, one step back
15

Detect request stream support in Bun and Deno, and cover the pipeline with testsSindre Sorhusbefore adoption83c711c

lines 16–20why, one step back
16 t.is(requests.length, 2);
17 t.is(error.request, requests.at(-1));18 t.is(error.request.headers.get('x-attempt'), '2');19 t.is(error.cause, cause);20});

Release the bodies a caller cannot reach and stop sharing search parametersSindre Sorhusbefore adoptionec3d17f

lines 21–54why, one step back
21
22test('beforeRetry sees the failed outgoing request while retaining its retry request', async t => {23 let failedRequest: Request | undefined;24 let hookCalls = 0;25 const result = await ky('https://example.com', {26 retry: {limit: 1, delay: () => 0},27 async fetch(request) {28 if (!failedRequest) {29 failedRequest = request;30 request.headers.set('x-attempt', 'failed');31 throw new TypeError('fetch failed');32 }3334 t.is(request.headers.get('x-retry'), 'yes');35 return new Response('ok');36 },37 hooks: {38 beforeRetry: [({error, request}) => {39 hookCalls++;40 t.true(isNetworkError(error));41 if (isNetworkError(error)) {42 t.is(error.request, failedRequest);43 t.is(error.request.headers.get('x-attempt'), 'failed');44 t.not(error.request, request);45 }4647 request.headers.set('x-retry', 'yes');48 }],49 },50 }).text();5152 t.is(result, 'ok');53 t.is(hookCalls, 1);54});

Support readonly inputs and explicit undefined in TypeScript optionsSindre Sorhusbefore adoption51b3177

lines 55–69why, one step back
55
56test('NetworkError preserves optional causes', t => {57 const request = new Request('https://example.com');5859 for (const cause of [undefined, new Error('Connection lost')]) {60 const error = new NetworkError(request, {cause});61 t.is(error.cause, cause);62 t.is(error.request, request);63 t.is(error.name, 'NetworkError');64 t.is(error.message, 'Request failed due to a network error: GET https://example.com/');65 t.true(isNetworkError(error));66 }6768 t.is(new NetworkError(request).cause, undefined);69});