Skip to content
Myr

HTTPError.ts

This file

Path

source/errors/HTTPError.ts

34 lines · 2.2 KiB · TypeScript

Last changed by

Readme tweaksSindre Sorhusbefore adoption7f2eade

History of this file

Take it

The walk stopped after 200 commits: lines older than 00e45d0 point at it.

Fix compatibility with native Fetch being disabled on Node.js (#494)三咲智子 Kevin Dengbefore adoption00e45d0

line 1why, one step back
1import type {NormalizedOptions} from '../types/options.js';

Use KyRequest and KyResponse types in errors (#610)Mark Fultonbefore adoption8e171f5

lines 2–3why, one step back
2import type {KyRequest} from '../types/request.js';
3import type {KyResponse} from '../types/response.js';

Make `beforeError` hook receive all errors (#829)Sindre Sorhusbefore adoption101c74b

line 4why, one step back
4import {KyError} from './KyError.js';

Fix compatibility with native Fetch being disabled on Node.js (#494)三咲智子 Kevin Dengbefore adoption00e45d0

line 5why, one step back
5

TweaksSindre Sorhusbefore adoption191057d

lines 6–8why, one step back
6/**
7Error thrown when the response has a non-2xx status code and `throwHttpErrors` is enabled.8

Readme tweaksSindre Sorhusbefore adoption7f2eade

lines 9–13why, one step back
9The error has a `response` property with the `Response` object, a `request` property with the `Request` object, an `options` property with the normalized options (either passed to `ky` when creating an instance with `ky.create()` or directly when performing the request), and a `data` property with the pre-parsed response body. For JSON responses (based on `Content-Type`), the body is parsed using the `parseJson` option if set, or `JSON.parse` by default. For other content types, it is set as plain text. If the body is empty or parsing fails, `data` will be `undefined`. To avoid hanging or excessive buffering, `error.data` population is bounded by the request timeout and a 10 MiB response body size limit. The `data` property is populated before `beforeError` hooks run, so hooks can access it.
1011The response body is automatically consumed when populating `error.data`, so `error.response.json()` and other body methods will not work. Use `error.data` instead. The `error.response` object is still available for headers, status, etc.1213Be aware that some types of errors, such as network errors, inherently mean that a response was not received. In that case, the error will be an instance of `NetworkError` instead of `HTTPError` and will not contain a `response` property.

TweaksSindre Sorhusbefore adoption191057d

line 14why, one step back
14*/

Make `beforeError` hook receive all errors (#829)Sindre Sorhusbefore adoption101c74b

lines 15–20why, one step back
15export class HTTPError<T = unknown> extends KyError {
16 override name = 'HTTPError' as const;17 response: KyResponse<T>;18 request: KyRequest;19 options: NormalizedOptions;20 data: T | string | undefined;

Fix compatibility with native Fetch being disabled on Node.js (#494)三咲智子 Kevin Dengbefore adoption00e45d0

lines 21–23why, one step back
21
22 constructor(response: Response, request: Request, options: NormalizedOptions) {23 const code = (response.status || response.status === 0) ? response.status : '';

Minor tweak (#745)Hichembefore adoptionb9df6cf

line 24why, one step back
24 const title = response.statusText ?? '';

Fix compatibility with native Fetch being disabled on Node.js (#494)三咲智子 Kevin Dengbefore adoption00e45d0

lines 25–27why, one step back
25 const status = `${code} ${title}`.trim();
26 const reason = status ? `status code ${status}` : 'an unknown error';27

Add request method and URL to error messages (#595)Seth Holladaybefore adoption36d0bd3

line 28why, one step back
28 super(`Request failed with ${reason}: ${request.method} ${request.url}`);

Fix compatibility with native Fetch being disabled on Node.js (#494)三咲智子 Kevin Dengbefore adoption00e45d0

lines 29–34why, one step back
29
30 this.response = response;31 this.request = request;32 this.options = options;33 }34}