From bbca5620ab45dc8ae5f67b83417329ec9b4ebd87 Mon Sep 17 00:00:00 2001 From: Mikael Finstad Date: Fri, 4 Nov 2022 06:21:42 +0100 Subject: [PATCH] Fix incorrect documentation and add example (#462) --- readme.md | 14 +++++++++++++- source/types/hooks.ts | 2 +- 2 files changed, 14 insertions(+), 2 deletions(-) diff --git a/readme.md b/readme.md index e36aa06a..41ff59b4 100644 --- a/readme.md +++ b/readme.md @@ -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; @@ -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). diff --git a/source/types/hooks.ts b/source/types/hooks.ts index fa276bd0..2f91502b 100644 --- a/source/types/hooks.ts +++ b/source/types/hooks.ts @@ -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;