Skip to content

Commit

Permalink
Fix incorrect documentation and add example (#462)
Browse files Browse the repository at this point in the history
  • Loading branch information
mifi authored Nov 4, 2022
1 parent 430486b commit bbca562
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 2 deletions.
14 changes: 13 additions & 1 deletion readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -275,7 +275,7 @@ await ky('https://example.com', {
const {response} = error;
if (response && response.body) {
error.name = 'GitHubError';
error.message = `${response.body.message} (${response.statusCode})`;
error.message = `${response.body.message} (${response.status})`;
}

return error;
Expand Down Expand Up @@ -491,6 +491,18 @@ const text = await ky('https://example.com', options).text();

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).

If you need to read the actual response when an `HTTPError` has occurred, call the respective parser method on the response object. For example:

```js
try {
await ky('https://example.com').json();
} catch (error) {
if (error.name === 'HTTPError') {
const errorJson = await error.response.json();
}
}
```

### TimeoutError

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).
Expand Down
2 changes: 1 addition & 1 deletion source/types/hooks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ export interface Hooks {
const {response} = error;
if (response && response.body) {
error.name = 'GitHubError';
error.message = `${response.body.message} (${response.statusCode})`;
error.message = `${response.body.message} (${response.status})`;
}
return error;
Expand Down

0 comments on commit bbca562

Please sign in to comment.