Skip to content
Myr

Add `beforeError` hook (#417)

Before adoption: this commit came in with the history the project adopted, so no landing records it.

authored by Malolan B · committed by GitHub · parent 3e72832 · Browse files · 6 files+134 added−2 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Trailers

  • Co-authored-by: Sindre Sorhus <sindresorhus@gmail.com>

Diff stats

6 files+134 added−2 removed

  • tests:+66 added−1 removed
  • other:+68 added−1 removed

6 files+134 added−2 removed

modified:source/core/Ky.ts+9 added-1 removed
37 unmodified lines
3839 ky._decorateResponse(response);4041 if (!response.ok && ky._options.throwHttpErrors) {42 throw new HTTPError(response, ky.request, (ky._options as unknown) as NormalizedOptions);42 let error = new HTTPError(response, ky.request, (ky._options as unknown) as NormalizedOptions);4344 for (const hook of ky._options.hooks.beforeError) {45 // eslint-disable-next-line no-await-in-loop46 error = await hook(error);47 }4849 throw error;50 }5152 // If `onDownloadProgress` is passed, it uses the stream API internally53 /* istanbul ignore next */
56 unmodified lines
110 hooks: deepMerge<Required<Hooks>>(111 {112 beforeRequest: [],113 beforeRetry: [],114 beforeError: [],115 afterResponse: [],116 },117 options.hooks,118 ),
200 unmodified lines
modified:source/types/hooks.ts+31 added
modified:source/index.ts+1 added
modified:test/helpers/with-page.ts-1 removed
modified:test/hooks.ts+66 added
600 unmodified lines
601 .text();602603 t.is(response, expectedResponse);604});605606test('runs beforeError before throwing HTTPError', async t => {607 const server = await createHttpTestServer();608 server.post('/', (_request, response) => {609 response.status(500).send();610 });611612 await t.throwsAsync(613 ky.post(server.url, {614 hooks: {615 beforeError: [616 (error: HTTPError) => {617 const {response} = error;618619 if (response?.body) {620 error.name = 'GitHubError';621 error.message = `${response.statusText} --- (${response.status})`.trim();622 }623624 return error;625 },626 ],627 },628 }),629 {630 name: 'GitHubError',631 message: 'Internal Server Error --- (500)',632 },633 );634635 await server.close();636});637638test('beforeError can return promise which resolves to HTTPError', async t => {639 const server = await createHttpTestServer();640 const responseBody = {reason: 'github down'};641 server.post('/', (_request, response) => {642 response.status(500).send(responseBody);643 });644645 await t.throwsAsync(646 ky.post(server.url, {647 hooks: {648 beforeError: [649 async (error: HTTPError) => {650 const {response} = error;651 const body = await response.json() as {reason: string};652653 if (response?.body) {654 error.name = 'GitHubError';655 error.message = `${body.reason} --- (${response.status})`.trim();656 }657658 return error;659 },660 ],661 },662 }),663 {664 name: 'GitHubError',665 message: `${responseBody.reason} --- (500)`,666 },667 );668669 await server.close();670});
modified:readme.md+27 added
256 unmodified lines
257 }258});259```260261###### hooks.beforeError262263Type: `Function[]`\264Default: `[]`265266This hook enables you to modify the `HTTPError` right before it is thrown. The hook function receives a `HTTPError` as an argument and should return an instance of `HTTPError`.267268```js269import ky from 'ky';270271await ky('https://example.com', {272 hooks: {273 beforeError: [274 error => {275 const {response} = error;276 if (response && response.body) {277 error.name = 'GitHubError';278 error.message = `${response.body.message} (${response.statusCode})`;279 }280281 return error;282 }283 ]284 }285});286```287288###### hooks.afterResponse289290Type: `Function[]`\291Default: `[]`
329 unmodified lines