From 519d17e54d52d80e42834f698f43f85393a0c3b1 Mon Sep 17 00:00:00 2001 From: ialexi-bl <44489753+ialexi-bl@users.noreply.github.com> Date: Fri, 8 Jan 2021 18:08:16 +1000 Subject: [PATCH] Add `request` and `options` to `HTTPError` (#309) Co-authored-by: Seth Holladay Co-authored-by: Sindre Sorhus --- index.d.ts | 6 ++++-- index.js | 6 ++++-- index.test-d.ts | 8 +++++++- readme.md | 4 ++-- 4 files changed, 17 insertions(+), 7 deletions(-) diff --git a/index.d.ts b/index.d.ts index 98a84ac4..a8f7eb61 100644 --- a/index.d.ts +++ b/index.d.ts @@ -414,11 +414,13 @@ export interface ResponsePromise extends Promise { } /** -The error has a response property with the `Response` object. +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). */ declare class HTTPError extends Error { - constructor(response: Response); + constructor(response: Response, request: Request, options: NormalizedOptions); response: Response; + request: Request; + options: NormalizedOptions; } /** diff --git a/index.js b/index.js index acf9e877..85dd4a32 100644 --- a/index.js +++ b/index.js @@ -118,7 +118,7 @@ const retryAfterStatusCodes = [ const stop = Symbol('stop'); class HTTPError extends Error { - constructor(response) { + constructor(response, request, options) { // Set the message to the status text, such as Unauthorized, // with some fallbacks. This message should never be undefined. super( @@ -130,6 +130,8 @@ class HTTPError extends Error { ); this.name = 'HTTPError'; this.response = response; + this.request = request; + this.options = options; } } @@ -292,7 +294,7 @@ class Ky { this._decorateResponse(response); if (!response.ok && this._options.throwHttpErrors) { - throw new HTTPError(response); + throw new HTTPError(response, this.request, this._options); } // If `onDownloadProgress` is passed, it uses the stream API internally diff --git a/index.test-d.ts b/index.test-d.ts index caa24df8..13d2eb93 100644 --- a/index.test-d.ts +++ b/index.test-d.ts @@ -23,7 +23,13 @@ for (const method of requestMethods) { expectType(ky.create({})); expectType(ky.extend({})); -expectType>(new ky.HTTPError(new Response())); +expectType>( + new ky.HTTPError( + new Response(), + new Request('https://example.com'), + {} as NormalizedOptions + ) +); expectType>(new ky.TimeoutError(new Request('Test'))); ky(url, { diff --git a/readme.md b/readme.md index 7363c5da..396f93d1 100644 --- a/readme.md +++ b/readme.md @@ -450,11 +450,11 @@ 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). +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. +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