Skip to content
Myr

Add type guards for errors (#746)

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

authored by Hichem · committed by GitHub · parent ac3e614 · Browse files · 5 files+91 added−8 removed
This commit

State

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

Diff stats

5 files+91 added−8 removed

  • tests:+4 added−2 removed
  • other:+87 added−6 removed

5 files+91 added−8 removed

modified:source/core/Ky.ts+3 added-3 removed
1import {HTTPError} from '../errors/HTTPError.js';2import {TimeoutError} from '../errors/TimeoutError.js';2import type {3 Input,4 InternalOptions,5 NormalizedOptions,
7 unmodified lines
13import timeout, {type TimeoutOptions} from '../utils/timeout.js';14import delay from '../utils/delay.js';15import {type ObjectEntries} from '../utils/types.js';16import {findUnknownOptions, hasSearchParameters} from '../utils/options.js';17import {isHTTPError, isTimeoutError} from '../utils/type-guards.js';18import {19 maxSafeTimeout,20 responseTypes,21 stop,
236 unmodified lines
258259 protected _calculateRetryDelay(error: unknown) {260 this._retryCount++;261262 if (this._retryCount > this._options.retry.limit || error instanceof TimeoutError) {262 if (this._retryCount > this._options.retry.limit || isTimeoutError(error)) {263 throw error;264 }265266 if (error instanceof HTTPError) {266 if (isHTTPError(error)) {267 if (!this._options.retry.statusCodes.includes(error.response.status)) {268 throw error;269 }270
92 unmodified lines
added:source/utils/type-guards.ts+72 added
modified:source/index.ts+1 added
modified:test/hooks.ts+4 added-2 removed
1import test from 'ava';2import delay from 'delay';3import ky, {HTTPError} from '../source/index.js';3import ky, {HTTPError, isHTTPError} from '../source/index.js';4import {type Options} from '../source/types/options.js';5import {createHttpTestServer} from './helpers/create-http-test-server.js';67test('hooks can be async', async t => {
396 unmodified lines
404 hooks: {405 beforeRetry: [406 ({error, retryCount}) => {407 t.true(error instanceof HTTPError);408 t.true(isHTTPError(error));409 t.true(retryCount >= 1);410 },411 ],412 },
44 unmodified lines
457 await server.close();458});459460test('beforeRetry hook with parseJson and error.response.json()', async t => {460 t.plan(10);461 t.plan(11);462463 let requestCount = 0;464465 const server = await createHttpTestServer();
16 unmodified lines
482 hooks: {483 beforeRetry: [484 async ({error, retryCount}) => {485 t.true(error instanceof HTTPError);486 t.true(isHTTPError(error));487 t.is(error.message, `Request failed with status code 502 Bad Gateway: GET ${server.url}/`);488 t.true((error as HTTPError).response instanceof Response);489 t.deepEqual(await (error as HTTPError).response.json(), {awesome: true});490 t.is(retryCount, 1);
186 unmodified lines
modified:readme.md+11 added-3 removed
620 unmodified lines
621622If you need to read the actual response when an `HTTPError` has occurred, call the respective parser method on the response object. For example:623624```js625import { HTTPError } from "ky";625import {isKyError, isHTTPError, isTimeoutError} from 'ky';626627try {628 await ky('https://example.com').json();629} catch (error) {630 if (error instanceof HTTPError) {631 const errorJson = await error.response.json();630 if (isKyError(error)) {631 if (isHTTPError(error)) {632 console.log('Status:', error.response.status);633 const errorJson = await error.response.json();634 } else if (isTimeoutError(error)) {635 console.log('Timeout URL:', error.request.url);636 }637 } else {638 // Handle other errors639 console.log('Unknown error:', error);640 }641}642```643
148 unmodified lines