Skip to content

Commit 519d17e

Browse files
jabujsholladaysindresorhus
authoredJan 8, 2021
Add request and options to HTTPError (#309)
Co-authored-by: Seth Holladay <me@seth-holladay.com> Co-authored-by: Sindre Sorhus <sindresorhus@gmail.com>
1 parent 5da3da0 commit 519d17e

File tree

4 files changed

+17
-7
lines changed

4 files changed

+17
-7
lines changed
 

‎index.d.ts

+4-2
Original file line numberDiff line numberDiff line change
@@ -414,11 +414,13 @@ export interface ResponsePromise extends Promise<Response> {
414414
}
415415

416416
/**
417-
The error has a response property with the `Response` object.
417+
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).
418418
*/
419419
declare class HTTPError extends Error {
420-
constructor(response: Response);
420+
constructor(response: Response, request: Request, options: NormalizedOptions);
421421
response: Response;
422+
request: Request;
423+
options: NormalizedOptions;
422424
}
423425

424426
/**

‎index.js

+4-2
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,7 @@ const retryAfterStatusCodes = [
118118
const stop = Symbol('stop');
119119

120120
class HTTPError extends Error {
121-
constructor(response) {
121+
constructor(response, request, options) {
122122
// Set the message to the status text, such as Unauthorized,
123123
// with some fallbacks. This message should never be undefined.
124124
super(
@@ -130,6 +130,8 @@ class HTTPError extends Error {
130130
);
131131
this.name = 'HTTPError';
132132
this.response = response;
133+
this.request = request;
134+
this.options = options;
133135
}
134136
}
135137

@@ -292,7 +294,7 @@ class Ky {
292294
this._decorateResponse(response);
293295

294296
if (!response.ok && this._options.throwHttpErrors) {
295-
throw new HTTPError(response);
297+
throw new HTTPError(response, this.request, this._options);
296298
}
297299

298300
// If `onDownloadProgress` is passed, it uses the stream API internally

‎index.test-d.ts

+7-1
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,13 @@ for (const method of requestMethods) {
2323

2424
expectType<typeof ky>(ky.create({}));
2525
expectType<typeof ky>(ky.extend({}));
26-
expectType<InstanceType<typeof ky.HTTPError>>(new ky.HTTPError(new Response()));
26+
expectType<InstanceType<typeof ky.HTTPError>>(
27+
new ky.HTTPError(
28+
new Response(),
29+
new Request('https://example.com'),
30+
{} as NormalizedOptions
31+
)
32+
);
2733
expectType<InstanceType<typeof ky.TimeoutError>>(new ky.TimeoutError(new Request('Test')));
2834

2935
ky(url, {

‎readme.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -450,11 +450,11 @@ Type: `object`
450450

451451
### ky.HTTPError
452452

453-
Exposed for `instanceof` checks. The error has a `response` property with the [`Response` object](https://developer.mozilla.org/en-US/docs/Web/API/Response).
453+
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).
454454

455455
### ky.TimeoutError
456456

457-
The error thrown when the request times out.
457+
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).
458458

459459
### ky.stop
460460

0 commit comments

Comments
 (0)
Please sign in to comment.