UNPKG

guardz

Version:

A simple and lightweight TypeScript type guard library for runtime type validation.

56 lines (55 loc) 1.86 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.isError = void 0; const reportTypeGuardError_1 = require("../utils/reportTypeGuardError"); /** * Checks if a value is an Error object. * * This type guard validates that a value is an Error instance or a subclass of Error. * It's useful for error handling and validation in try-catch blocks. * * @param value - The value to check * @param config - Optional configuration for error handling * @returns True if the value is an Error object, false otherwise * * @example * ```typescript * import { isError } from 'guardz'; * * console.log(isError(new Error('Something went wrong'))); // true * console.log(isError(new TypeError('Invalid type'))); // true * console.log(isError(new ReferenceError('Not defined'))); // true * console.log(isError('error message')); // false (string) * console.log(isError(123)); // false (number) * console.log(isError({ message: 'error' })); // false (plain object) * * // With type narrowing * const data: unknown = getErrorData(); * if (isError(data)) { * // data is now typed as Error * console.log('Error message:', data.message); * console.log('Error stack:', data.stack); * } * * // With error handling * try { * // Some operation that might throw * throw new Error('Operation failed'); * } catch (error) { * if (isError(error, { identifier: 'caughtError' })) { * // error is now typed as Error * console.error('Caught error:', error.message); * } else { * console.error('Unknown error type:', error); * } * } * ``` */ const isError = function isErrorGuard(value, config) { if (!(value instanceof Error)) { (0, reportTypeGuardError_1.reportTypeGuardError)(config, value, 'Error'); return false; } return true; }; exports.isError = isError;