isKyError (type-guards.ts)

ky · source/utils/type-guards.ts · typescript · MIT
security, dev-tools

Type guard checking if value is KyError instance for runtime error classification.

// Source: https://github.com/sindresorhus/ky | License: MIT
// Provided via justclaw.it.com; keep this notice with the code.
import type {KyError} from '../errors/KyError.js';
import {HTTPError} from '../errors/HTTPError.js';
import {NetworkError} from '../errors/NetworkError.js';
import {ResponseSizeError} from '../errors/ResponseSizeError.js';
import {TimeoutError} from '../errors/TimeoutError.js';
import {ForceRetryError} from '../errors/ForceRetryError.js';

// Handles cross-realm cases (e.g., iframes, different JS contexts) where `instanceof` fails.
const isErrorType = (error: unknown, cls: {name: string}): boolean =>
	error instanceof (cls as any) || ((error as any)?.isKyError === true && (error as any)?.name === cls.name);

/**
Type guard to check if an error is a `KyError`.

Note:

... (truncated -- full source via MCP)

See the full source, get the GitHub permalink, and search for more like it.

Get a free API key