All files / src/utils/converter errorify.ts

100% Statements 3/3
100% Branches 3/3
100% Functions 1/1
100% Lines 3/3

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 201x                                 1x 26x  
import { stringify } from './stringify.ts';
 
/**
 * Converts a given value into an instance of Error if needed.
 *
 * If a new error is created, the message is computed using {@link stringify}.
 *
 * @example
 *
 * ```ts
 * import { errorify } from 'emitnlog/utils';
 * const error = errorify('An error occurred');
 * ```
 *
 * @param {unknown} value - The value to convert into an Error.
 * @returns {Error} An error object, either the specified input or a new one created by this method.
 */
export const errorify = (value: unknown): Error =>
  value instanceof Error ? value : new Error(stringify(value), { cause: value });