Skip to content

Open source website builder and Webflow alternative. Webstudio is an advanced visual builder that connects to any headless CMS, supports all CSS properties, and can be hosted anywhere, including with us.

641 B
Raw Blame
import { TRPCError } from "@trpc/server";

type ApiErrorOptions = {
  webstudioCode?: string;
  issues?: unknown;
};

export const throwApiError = (
  code: "BAD_REQUEST" | "NOT_FOUND" | "CONFLICT",
  message: string,
  options: ApiErrorOptions = {}
): never => {
  const errorOptions =
    code === "NOT_FOUND" && options.webstudioCode === undefined
      ? { ...options, webstudioCode: code }
      : options;
  const cause =
    errorOptions.webstudioCode === undefined &&
    errorOptions.issues === undefined
      ? undefined
      : Object.assign(new Error(message), errorOptions);
  throw new TRPCError({ code, message, cause });
};