HTTPError.ts
This file
Path
source/errors/HTTPError.ts
34 lines · 2.6 KiB · TypeScript
Last changed by
Add `maxResponseSize` optionSindre Sorhusbefore adoption0d59458
History of this fileTake it
The walk stopped after 200 commits: lines older than 37fc250 point at it.
Add retry count to `afterResponse` and `beforeError` hooks (#763)Sindre Sorhusbefore adoption37fc250
lines 1–3why, one step back1import type {NormalizedOptions} from '../types/options.js';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 back4import {KyError} from './KyError.js';Add retry count to `afterResponse` and `beforeError` hooks (#763)Sindre Sorhusbefore adoption37fc250
line 5why, one step back5TweaksSindre Sorhusbefore adoption191057d
lines 6–8why, one step back6/**7Error thrown when the response has a non-2xx status code and `throwHttpErrors` is enabled.8Add `maxResponseSize` optionSindre Sorhusbefore adoption0d59458
line 9why, one step back9The 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, unreadable, too large, parsing fails, or the error-data read/parse timeout is reached, `data` will be `undefined`. To avoid hanging or excessive buffering, `error.data` body reads and async JSON parsing are bounded by the request timeout (or 10 seconds when `timeout` is disabled), any remaining `totalTimeout` budget, and a 10 MiB response body size limit. If `maxResponseSize` is exceeded while populating `error.data`, Ky throws `ResponseSizeError` instead of `HTTPError`. If `totalTimeout` expires while populating `error.data`, Ky throws `TimeoutError` instead of `HTTPError`. The `data` property is populated before `beforeError` hooks run, so hooks can access it.Readme tweaksSindre Sorhusbefore adoption7f2eade
lines 10–13why, one step back1011The 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 back14*/Make `beforeError` hook receive all errors (#829)Sindre Sorhusbefore adoption101c74b
line 15why, one step back15export class HTTPError<T = unknown> extends KyError {Allow customizing error names in TypeScriptSindre Sorhusbefore adoption52b8f93
line 16why, one step back16 override name = 'HTTPError';Make `beforeError` hook receive all errors (#829)Sindre Sorhusbefore adoption101c74b
lines 17–18why, one step back17 response: KyResponse<T>;18 request: KyRequest;Harden retrySindre Sorhusbefore adoptionc426f19
line 19why, one step back19 options: Readonly<NormalizedOptions>;Make `beforeError` hook receive all errors (#829)Sindre Sorhusbefore adoption101c74b
line 20why, one step back20 data: T | string | undefined;Add retry count to `afterResponse` and `beforeError` hooks (#763)Sindre Sorhusbefore adoption37fc250
line 21why, one step back21Harden retrySindre Sorhusbefore adoptionc426f19
line 22why, one step back22 constructor(response: Response, request: Request, options: Readonly<NormalizedOptions>) {Add retry count to `afterResponse` and `beforeError` hooks (#763)Sindre Sorhusbefore adoption37fc250
lines 23–34why, one step back23 const code = (response.status || response.status === 0) ? response.status : '';24 const title = response.statusText ?? '';25 const status = `${code} ${title}`.trim();26 const reason = status ? `status code ${status}` : 'an unknown error';2728 super(`Request failed with ${reason}: ${request.method} ${request.url}`);2930 this.response = response;31 this.request = request;32 this.options = options;33 }34}