/**
 * A readable string for anything that was thrown.
 *
 * `useClient` rejects with a plain `{ status, message }` object, not an `Error`, so the reflex
 * `err instanceof Error ? err.message : …` misses the most common failure in this app entirely and
 * reports every API error as "unknown error". Check the shape first, the class second.
 *
 * Truncated because these end up in toasts, and a server that answers a failure with a wall of HTML
 * should not push the rest of the screen off it.
 */
export function errorText(err: unknown, fallback = 'unknown error'): string {
  if (typeof err === 'object' && err !== null && 'message' in err) {
    const message = (err as { message: unknown }).message;
    if (typeof message === 'string' && message.trim()) return message.slice(0, 200);
  }
  if (typeof err === 'string' && err.trim()) return err.slice(0, 200);
  return fallback;
}
