HTTPError.ts
This file
Path
source/errors/HTTPError.ts
24 lines · 867 bytes · TypeScript
Last changed by
Add `data` property to `HTTPError` with pre-parsed response body (#823)Sindre Sorhusbefore adoption1341f5c
History of this fileTake it
12345678910111213141516171819202122232425import type {NormalizedOptions} from '../types/options.js';import type {KyRequest} from '../types/request.js';import type {KyResponse} from '../types/response.js';
export class HTTPError<T = unknown> extends Error { public response: KyResponse<T>; public request: KyRequest; public options: NormalizedOptions; public data: T | string | undefined;
constructor(response: Response, request: Request, options: NormalizedOptions) { const code = (response.status || response.status === 0) ? response.status : ''; const title = response.statusText ?? ''; const status = `${code} ${title}`.trim(); const reason = status ? `status code ${status}` : 'an unknown error';
super(`Request failed with ${reason}: ${request.method} ${request.url}`);
this.name = 'HTTPError'; this.response = response; this.request = request; this.options = options; }}