Skip to content
Myr

Make `HTTPError` and `TimeoutError` named exports

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Fixes #320

authored by Sindre Sorhus · committed by Sindre Sorhus · parent b3c9e88 · Browse files · 6 files+21 added−27 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

6 files+21 added−27 removed

  • tests:+10 added−11 removed
  • other:+11 added−16 removed

6 files+21 added−27 removed

modified:source/types/ky.ts-4 removed
modified:source/index.ts+3 added-4 removed
modified:test/hooks.ts+3 added-4 removed
1import test from 'ava';2import delay from 'delay';3import {HTTPError} from '../source/errors/HTTPError.js';4import ky from '../source/index.js';3import ky, {HTTPError} from '../source/index.js';4import {createHttpTestServer} from './helpers/create-http-test-server.js';56test('hooks can be async', async t => {7 const server = await createHttpTestServer();
395 unmodified lines
403 await ky.get(server.url, {404 hooks: {405 beforeRetry: [406 ({error, retryCount}) => {408 t.true(error instanceof ky.HTTPError);407 t.true(error instanceof HTTPError);408 t.true(retryCount >= 1);409 }410 ]411 }
68 unmodified lines
480 },481 hooks: {482 beforeRetry: [483 async ({error, retryCount}) => {485 t.true(error instanceof ky.HTTPError);484 t.true(error instanceof HTTPError);485 t.is(error.message, 'Request failed with status code 502 Bad Gateway');486 t.true((error as HTTPError).response instanceof Response);487 t.deepEqual(await (error as HTTPError).response.json(), {awesome: true});488 t.is(retryCount, 1);
116 unmodified lines
modified:test/http-error.ts+4 added-4 removed
1import test from 'ava';2import ky from '../source/index.js';2import {HTTPError} from '../source/index.js';34function createFakeResponse({status, statusText}: {status?: number; statusText?: string}): Response {5 // Start with a realistic fetch Response.6 const response = {...new Response()};
10 unmodified lines
1718test('HTTPError handles undefined response.statusText', t => {19 const status = 500;20 // @ts-expect-error missing Request21 const error = new ky.HTTPError(21 const error = new HTTPError(22 // This simulates the case where a browser Response object does23 // not define statusText, such as IE, Safari, etc.24 // See https://developer.mozilla.org/en-US/docs/Web/API/Response/statusText#Browser_compatibility25 createFakeResponse({statusText: undefined, status})
3 unmodified lines
29});3031test('HTTPError handles undefined response.status', t => {32 // @ts-expect-error missing Request33 const error = new ky.HTTPError(33 const error = new HTTPError(34 // This simulates a catastrophic case where some unexpected35 // response object was sent to HTTPError.36 createFakeResponse({statusText: undefined, status: undefined})37 );
2 unmodified lines
40});4142test('HTTPError handles a response.status of 0', t => {43 // @ts-expect-error missing Request44 const error = new ky.HTTPError(44 const error = new HTTPError(45 // Apparently, it's possible to get a response status of 0.46 createFakeResponse({statusText: undefined, status: 0})47 );48
2 unmodified lines
modified:test/main.ts+3 added-3 removed
1import test from 'ava';2import delay from 'delay';3import ky from '../source/index.js';3import ky, {TimeoutError} from '../source/index.js';4import {createHttpTestServer} from './helpers/create-http-test-server.js';5import {parseRawBody} from './helpers/parse-body.js';67const fixture = 'fixture';
268 unmodified lines
276 response.end(fixture);277 });278279 await t.throwsAsync(ky(server.url, {timeout: 1000}).text(), {280 instanceOf: ky.TimeoutError280 instanceOf: TimeoutError281 });282283 t.is(requestCount, 1);284
119 unmodified lines
404 });405406 await t.throwsAsync(407 ky(server.url, {throwHttpErrors: false, timeout: 500}).text(),408 {instanceOf: ky.TimeoutError}408 {instanceOf: TimeoutError}409 );410411 t.is(requestCount, 1);412
279 unmodified lines
modified:readme.md+8 added-8 removed
423 unmodified lines
424#### defaultOptions425426Type: `object`427428### ky.HTTPError429430Exposed for `instanceof` checks. The error has a `response` property with the [`Response` object](https://developer.mozilla.org/en-US/docs/Web/API/Response), `request` property with the [`Request` object](https://developer.mozilla.org/en-US/docs/Web/API/Request), and `options` property with normalized options (either passed to `ky` when creating an instance with `ky.create()` or directly when performing the request).431432### ky.TimeoutError433434The error thrown when the request times out. It has a `request` property with the [`Request` object](https://developer.mozilla.org/en-US/docs/Web/API/Request).435428### ky.stop429430A `Symbol` that can be returned by a `beforeRetry` hook to stop the retry. This will also short circuit the remaining `beforeRetry` hooks.431
23 unmodified lines
455// Using `.text()` or other body methods is not suppported.456const text = await ky('https://example.com', options).text();457```458459### HTTPError460461Exposed for `instanceof` checks. The error has a `response` property with the [`Response` object](https://developer.mozilla.org/en-US/docs/Web/API/Response), `request` property with the [`Request` object](https://developer.mozilla.org/en-US/docs/Web/API/Request), and `options` property with normalized options (either passed to `ky` when creating an instance with `ky.create()` or directly when performing the request).462463### TimeoutError464465The error thrown when the request times out. It has a `request` property with the [`Request` object](https://developer.mozilla.org/en-US/docs/Web/API/Request).466467## Tips468469### Sending form data470
119 unmodified lines