Skip to content

Commit

Permalink
Add request and options to HTTPError (#309)
Browse files Browse the repository at this point in the history
Co-authored-by: Seth Holladay <me@seth-holladay.com>
Co-authored-by: Sindre Sorhus <sindresorhus@gmail.com>
  • Loading branch information
3 people committed Jan 8, 2021
1 parent 5da3da0 commit 519d17e
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 7 deletions.
6 changes: 4 additions & 2 deletions index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -414,11 +414,13 @@ export interface ResponsePromise extends Promise<Response> {
}

/**
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;
}

/**
Expand Down
6 changes: 4 additions & 2 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand All @@ -130,6 +130,8 @@ class HTTPError extends Error {
);
this.name = 'HTTPError';
this.response = response;
this.request = request;
this.options = options;
}
}

Expand Down Expand Up @@ -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
Expand Down
8 changes: 7 additions & 1 deletion index.test-d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,13 @@ for (const method of requestMethods) {

expectType<typeof ky>(ky.create({}));
expectType<typeof ky>(ky.extend({}));
expectType<InstanceType<typeof ky.HTTPError>>(new ky.HTTPError(new Response()));
expectType<InstanceType<typeof ky.HTTPError>>(
new ky.HTTPError(
new Response(),
new Request('https://example.com'),
{} as NormalizedOptions
)
);
expectType<InstanceType<typeof ky.TimeoutError>>(new ky.TimeoutError(new Request('Test')));

ky(url, {
Expand Down
4 changes: 2 additions & 2 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down

0 comments on commit 519d17e

Please sign in to comment.