Skip to content
Myr

Ignore non-Errors returned by beforeError hooks (#833)

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

authored by 이진혁 · committed by GitHub · parent c168a7c · Browse files · 2 files+25 added−1 removed
This commit

State

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

Diff stats

2 files+25 added−1 removed

  • tests:+19 added−0 removed
  • other:+6 added−1 removed

2 files+25 added−1 removed

modified:source/core/Ky.ts+6 added-1 removed
159 unmodified lines
160161 let processedError: Error = error;162 for (const hook of ky.#options.hooks.beforeError) {163 // eslint-disable-next-line no-await-in-loop164 processedError = await hook({164 const hookResult: unknown = await hook({165 error: processedError,166 retryCount,167 });168169 // Only overwrite if the hook returns a valid Error instance.170 if (hookResult instanceof Error) {171 processedError = hookResult;172 }173 }174175 throw processedError;176 } finally {
595 unmodified lines
modified:test/hooks.ts+19 added
1294 unmodified lines
1295 },1296 );1297});12981299test('beforeError ignores non-Error return values from hooks', async t => {1300 const server = await createHttpTestServer(t);13011302 server.post('/', (_request, response) => {1303 response.status(500).send();1304 });13051306 await t.throwsAsync(1307 ky.post(server.url, {1308 hooks: {1309 beforeError: [1310 (() => undefined as unknown as Error) as any,1311 ],1312 },1313 }),1314 {instanceOf: HTTPError},1315 );1316});13171318test('beforeRequest hook receives retryCount parameter', async t => {1319 let requestCount = 0;1320 const retryCounts: number[] = [];1321
1865 unmodified lines