/**
 * Utility functions for consistent error handling across the application.
 */

/**
 * Extracts a user-friendly error message from an unknown error.
 * Handles Error objects, strings, and other types gracefully.
 *
 * @param err - The caught error (can be any type)
 * @param fallback - Fallback message if error message cannot be extracted
 * @returns A user-friendly error message string
 *
 * @example
 * ```ts
 * try {
 *   await apiCall();
 * } catch (err) {
 *   setError(getErrorMessage(err, 'Failed to load data'));
 * }
 * ```
 */
export function getErrorMessage(err: unknown, fallback = 'An error occurred'): string {
  if (err instanceof Error) {
    return err.message;
  }
  if (typeof err === 'string') {
    return err;
  }
  return fallback;
}
