import { TRPCError } from "@trpc/server";

export async function findUniqueOrThrow<T>(
  query: Promise<T | null>,
  entityName: string,
): Promise<T> {
  const result = await query;
  if (!result) {
    throw new TRPCError({ code: "NOT_FOUND", message: `${entityName} not found` });
  }
  return result;
}
