/** Client-safe error type thrown by server fetch helpers. */
export class ServerFetchError extends Error {
  public readonly response: Response | undefined;

  constructor(message?: string, response?: Response) {
    super(message);
    this.response = response;
    Object.setPrototypeOf(this, ServerFetchError.prototype);
  }
}

/** Works in client components after server actions (errors may lose prototype). */
export const isNotFoundServerFetchError = (e: unknown): boolean => {
  if (e instanceof ServerFetchError) {
    return e.response?.status === 404;
  }
  if (e instanceof Error) {
    return /\b404\b/.test(e.message);
  }
  return false;
};
