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.
TypeScript accessibilityalternativecloudflarecloudflare-workersdesignfreefrontendopen-sourceradix-uireactremixweb-developmentwebflowwebsite-builderwebsite-developmentwebsiteswebstudio
import { TRPCError } from "@trpc/server";
import { fetchRequestHandler } from "@trpc/server/adapters/fetch";
import { expect, test } from "vitest";
import type { AppContext } from "./context.server";
import { procedure, router } from "./router.server";
class ProjectNotPublishedError extends Error {
webstudioCode = "PROJECT_NOT_PUBLISHED" as const;
}
const createContext = (): AppContext =>
({
apiClient: { type: "cli", version: undefined },
trpcCache: {
setMaxAge: () => {},
getMaxAge: () => undefined,
},
}) as unknown as AppContext;
test("includes webstudio error codes in formatted tRPC errors", async () => {
const appRouter = router({
fail: procedure.query(() => {
throw new ProjectNotPublishedError("Not published");
}),
});
const response = await fetchRequestHandler({
endpoint: "/trpc",
req: new Request("http://localhost/trpc/fail"),
router: appRouter,
createContext,
});
expect(response.status).toBe(500);
await expect(response.json()).resolves.toMatchObject({
error: {
data: {
code: "INTERNAL_SERVER_ERROR",
webstudioCode: "PROJECT_NOT_PUBLISHED",
},
},
});
});
test("includes structured validation issues in formatted tRPC errors", async () => {
const issue = {
code: "invalid_type",
path: ["name"],
message: "Expected string",
constraint: "type:string",
};
const appRouter = router({
fail: procedure.query(() => {
throw new TRPCError({
code: "BAD_REQUEST",
cause: Object.assign(new Error("Invalid input"), {
webstudioCode: "INVALID_INPUT",
issues: [issue],
}),
});
}),
});
const response = await fetchRequestHandler({
endpoint: "/trpc",
req: new Request("http://localhost/trpc/fail"),
router: appRouter,
createContext,
});
expect(response.status).toBe(400);
await expect(response.json()).resolves.toMatchObject({
error: {
data: {
code: "BAD_REQUEST",
webstudioCode: "INVALID_INPUT",
issues: [issue],
},
},
});
});
test("does not classify a domain not-found error as API incompatibility", async () => {
const appRouter = router({
fail: procedure.query(() => {
throw new TRPCError({
code: "NOT_FOUND",
cause: Object.assign(new Error("Index was not found"), {
webstudioCode: "ASSET_RESOURCE_INDEX_NOT_FOUND",
}),
});
}),
});
const response = await fetchRequestHandler({
endpoint: "/trpc",
req: new Request("http://localhost/trpc/fail"),
router: appRouter,
createContext,
});
expect(response.status).toBe(404);
const body = await response.json();
expect(body).toMatchObject({
error: {
data: {
code: "NOT_FOUND",
webstudioCode: "ASSET_RESOURCE_INDEX_NOT_FOUND",
},
},
});
expect(JSON.stringify(body)).not.toContain("apiCompatibility");
});
test("preserves missing import filenames independently of error wording", async () => {
const appRouter = router({
fail: procedure.mutation(() => {
throw Object.assign(new Error("Import files are unavailable"), {
webstudioCode: "MISSING_IMPORTED_ASSET_FILES",
missingAssetNames: ["font,latin.woff2"],
});
}),
});
const response = await fetchRequestHandler({
endpoint: "/trpc",
req: new Request("http://localhost/trpc/fail", {
method: "POST",
headers: { "content-type": "application/json" },
body: "null",
}),
router: appRouter,
createContext,
});
await expect(response.json()).resolves.toMatchObject({
error: {
data: {
webstudioCode: "MISSING_IMPORTED_ASSET_FILES",
missingAssetNames: ["font,latin.woff2"],
},
},
});
});