/**
 * Throws an error with the provided message or generated by a function.
 * @param {function | string | Error} err - The error message, function returning an error, or an Error object.
 */
const panic = (error) => {
  if (typeof error === 'function') {
    return panic(error());
  }

  throw error instanceof Error ? error : new Error(error);
};

/**
 * Wraps a function to prefix its error messages with a specified string.
 * If an error is thrown by the wrapped function, this error will be caught,
 * modified to include the provided prefix in its message, and then rethrown.
 *
 * @param {function(): T} fn - The function to wrap
 * @param {string} prefix - Prefix to add before the original error message
 * @returns {T} The return value of the wrapped function
 *
 * @example
 * const wrappedFn = catchErrorWith('Error in component', () => {
 *   throw new Error('Something went wrong');
 * });
 *
 * try {
 *   wrappedFn();
 * } catch (err) {
 *   console.error(err.message); // "Error in component: Something went wrong"
 * }
 */
const catchErrorWith = (prefix, fn) => {
  try {
    return fn();
  } catch (err) {
    err.message = `${prefix}: ${err.message}`;

    throw err;
  }
};

/**
 * Creates a function that will prefix any error messages from the original
 * function call with the specified string. This is useful for adding context
 * to errors thrown by functions that don't directly handle their own error messaging.
 *
 * @param {function} fn - The function to wrap
 * @param {string} prefix - Prefix to add before any error messages
 * @returns {function(...args): T} A new function that will execute the original function with provided arguments and prefix its errors
 */
const catchFnErrorWith = (prefix, fn) => (...args) => catchErrorWith(prefix, () => fn(...args));

export { catchErrorWith, catchFnErrorWith, panic };
//# sourceMappingURL=error.js.map
