Skip to content

Commit

Permalink
Make HTTPError and TimeoutError named exports
Browse files Browse the repository at this point in the history
Fixes #320
  • Loading branch information
sindresorhus committed May 4, 2021
1 parent b3c9e88 commit 6ec7fd7
Show file tree
Hide file tree
Showing 6 changed files with 21 additions and 27 deletions.
16 changes: 8 additions & 8 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -425,14 +425,6 @@ const response = await api.get('/status', {prefixUrl: ''});

Type: `object`

### ky.HTTPError

Exposed 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).

### ky.TimeoutError

The 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).

### ky.stop

A `Symbol` that can be returned by a `beforeRetry` hook to stop the retry. This will also short circuit the remaining `beforeRetry` hooks.
Expand Down Expand Up @@ -464,6 +456,14 @@ const response = await ky.post('https://example.com', options);
const text = await ky('https://example.com', options).text();
```

### HTTPError

Exposed 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).

### TimeoutError

The 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).

## Tips

### Sending form data
Expand Down
7 changes: 3 additions & 4 deletions source/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@

import {Ky} from './core/Ky.js';
import {requestMethods, stop} from './core/constants.js';
import {HTTPError} from './errors/HTTPError.js';
import {TimeoutError} from './errors/TimeoutError.js';
import type {ky as KyInterface} from './types/ky.js';
import type {Input, Options} from './types/options.js';
import {validateAndMerge} from './utils/merge.js';
Expand All @@ -18,8 +16,6 @@ const createInstance = (defaults?: Partial<Options>): KyInterface => {
ky[method] = (input: Input, options: Options) => Ky.create(input, validateAndMerge(defaults, options, {method}));
}

ky.HTTPError = HTTPError;
ky.TimeoutError = TimeoutError;
ky.create = (newDefaults?: Partial<Options>) => createInstance(validateAndMerge(newDefaults));
ky.extend = (newDefaults?: Partial<Options>) => createInstance(validateAndMerge(defaults, newDefaults));
ky.stop = stop;
Expand All @@ -30,3 +26,6 @@ const createInstance = (defaults?: Partial<Options>): KyInterface => {
const ky = createInstance();

export default ky;

export {HTTPError} from './errors/HTTPError.js';
export {TimeoutError} from './errors/TimeoutError.js';
4 changes: 0 additions & 4 deletions source/types/ky.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
import {stop} from '../core/constants.js';
import type {HTTPError} from '../errors/HTTPError.js';
import type {TimeoutError} from '../errors/TimeoutError.js';
import type {Input, Options} from './options.js';
import type {ResponsePromise} from './response.js';

Expand Down Expand Up @@ -101,8 +99,6 @@ export interface ky {
```
*/
readonly stop: typeof stop;
readonly TimeoutError: typeof TimeoutError;
readonly HTTPError: typeof HTTPError;

/**
Fetch the given `url`.
Expand Down
7 changes: 3 additions & 4 deletions test/hooks.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import test from 'ava';
import delay from 'delay';
import {HTTPError} from '../source/errors/HTTPError.js';
import ky from '../source/index.js';
import ky, {HTTPError} from '../source/index.js';
import {createHttpTestServer} from './helpers/create-http-test-server.js';

test('hooks can be async', async t => {
Expand Down Expand Up @@ -405,7 +404,7 @@ test('beforeRetry hook is called with error and retryCount', async t => {
hooks: {
beforeRetry: [
({error, retryCount}) => {
t.true(error instanceof ky.HTTPError);
t.true(error instanceof HTTPError);
t.true(retryCount >= 1);
}
]
Expand Down Expand Up @@ -482,7 +481,7 @@ test('beforeRetry hook with parseJson and error.response.json()', async t => {
hooks: {
beforeRetry: [
async ({error, retryCount}) => {
t.true(error instanceof ky.HTTPError);
t.true(error instanceof HTTPError);
t.is(error.message, 'Request failed with status code 502 Bad Gateway');
t.true((error as HTTPError).response instanceof Response);
t.deepEqual(await (error as HTTPError).response.json(), {awesome: true});
Expand Down
8 changes: 4 additions & 4 deletions test/http-error.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import test from 'ava';
import ky from '../source/index.js';
import {HTTPError} from '../source/index.js';

function createFakeResponse({status, statusText}: {status?: number; statusText?: string}): Response {
// Start with a realistic fetch Response.
Expand All @@ -18,7 +18,7 @@ function createFakeResponse({status, statusText}: {status?: number; statusText?:
test('HTTPError handles undefined response.statusText', t => {
const status = 500;
// @ts-expect-error missing Request
const error = new ky.HTTPError(
const error = new HTTPError(
// This simulates the case where a browser Response object does
// not define statusText, such as IE, Safari, etc.
// See https://developer.mozilla.org/en-US/docs/Web/API/Response/statusText#Browser_compatibility
Expand All @@ -30,7 +30,7 @@ test('HTTPError handles undefined response.statusText', t => {

test('HTTPError handles undefined response.status', t => {
// @ts-expect-error missing Request
const error = new ky.HTTPError(
const error = new HTTPError(
// This simulates a catastrophic case where some unexpected
// response object was sent to HTTPError.
createFakeResponse({statusText: undefined, status: undefined})
Expand All @@ -41,7 +41,7 @@ test('HTTPError handles undefined response.status', t => {

test('HTTPError handles a response.status of 0', t => {
// @ts-expect-error missing Request
const error = new ky.HTTPError(
const error = new HTTPError(
// Apparently, it's possible to get a response status of 0.
createFakeResponse({statusText: undefined, status: 0})
);
Expand Down
6 changes: 3 additions & 3 deletions test/main.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import test from 'ava';
import delay from 'delay';
import ky from '../source/index.js';
import ky, {TimeoutError} from '../source/index.js';
import {createHttpTestServer} from './helpers/create-http-test-server.js';
import {parseRawBody} from './helpers/parse-body.js';

Expand Down Expand Up @@ -277,7 +277,7 @@ test('timeout option', async t => {
});

await t.throwsAsync(ky(server.url, {timeout: 1000}).text(), {
instanceOf: ky.TimeoutError
instanceOf: TimeoutError
});

t.is(requestCount, 1);
Expand Down Expand Up @@ -405,7 +405,7 @@ test('throwHttpErrors:false does not suppress timeout errors', async t => {

await t.throwsAsync(
ky(server.url, {throwHttpErrors: false, timeout: 500}).text(),
{instanceOf: ky.TimeoutError}
{instanceOf: TimeoutError}
);

t.is(requestCount, 1);
Expand Down

0 comments on commit 6ec7fd7

Please sign in to comment.