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.

Unify and simplify fetch and errors for endpoints and packages used in SaaS (#930)

Ivan Starkov119f1cf78c02
diff --git a/apps/designer/app/routes/rest/project.$projectId.tsx b/apps/designer/app/routes/rest/project.$projectId.tsx
index 8b3f7655d3..96a7795281 100644
--- a/apps/designer/app/routes/rest/project.$projectId.tsx
+++ b/apps/designer/app/routes/rest/project.$projectId.tsx
@@ -1,14 +1,16 @@
import { json, type LoaderArgs } from "@remix-run/node";
-import env from "~/env.server";
import { db } from "@webstudio-is/project/server";
import { sentryException } from "~/shared/sentry";
import { loadCanvasData } from "~/shared/db";
import type { CanvasData } from "@webstudio-is/project";
import { createContext } from "~/shared/context.server";
-type PagesDetails = Array<CanvasData | undefined>;
+type PagesDetails = Array<CanvasData>;
-export const loader = async ({ params, request }: LoaderArgs) => {
+export const loader = async ({
+ params,
+ request,
+}: LoaderArgs): Promise<PagesDetails> => {
try {
const projectId = params.projectId ?? undefined;
const pages: PagesDetails = [];
@@ -36,6 +38,7 @@ export const loader = async ({ params, request }: LoaderArgs) => {
const canvasData = await loadCanvasData(project, "prod", homePage.path);
pages.push(canvasData);
+
if (otherPages.length > 0) {
for (const page of otherPages) {
const canvasData = await loadCanvasData(project, "prod", page.path);
@@ -52,9 +55,10 @@ export const loader = async ({ params, request }: LoaderArgs) => {
}
sentryException({ error });
- return {
- errors: error instanceof Error ? error.message : String(error),
- env,
- };
+
+ // We have no idea what happened, so we'll return a 500 error.
+ throw json(error instanceof Error ? error.message : String(error), {
+ status: 500,
+ });
}
};
diff --git a/apps/designer/app/shared/db/canvas.server.ts b/apps/designer/app/shared/db/canvas.server.ts
index ed2129e654..3f81b217a8 100644
--- a/apps/designer/app/shared/db/canvas.server.ts
+++ b/apps/designer/app/shared/db/canvas.server.ts
@@ -7,7 +7,7 @@ export const loadCanvasData = async (
project: Project,
env: "dev" | "prod",
pageIdOrPath = ""
-): Promise<CanvasData | undefined> => {
+): Promise<CanvasData> => {
const build =
env === "dev"
? await projectDb.build.loadByProjectId(project.id, "dev")
diff --git a/packages/http-client/src/index.ts b/packages/http-client/src/index.ts
index 7aef686efb..09a3dd6392 100644
--- a/packages/http-client/src/index.ts
+++ b/packages/http-client/src/index.ts
@@ -7,23 +7,20 @@ export const loadProject = async ({
}: {
apiUrl: string;
projectId: string;
-}): Promise<Array<CanvasData> | string> => {
- try {
- if (apiUrl === undefined) {
- throw new Error("Webstudio API URL is required.");
- }
- const baseUrl = new URL(apiUrl);
- const projectUrl = new URL(`/rest/project/${projectId}`, baseUrl);
- const projectResponse = await fetch(projectUrl);
- const project = await projectResponse.json();
- if (!projectResponse.ok) {
- throw new Error(project);
- }
- return project;
- } catch (error) {
- if (error instanceof Error) {
- return error.message;
- }
- throw error;
+}): Promise<Array<CanvasData>> => {
+ if (apiUrl === undefined) {
+ throw new Error("Webstudio API URL is required.");
}
+ const baseUrl = new URL(apiUrl);
+ const projectUrl = new URL(`/rest/project/${projectId}`, baseUrl);
+ const projectResponse = await fetch(projectUrl);
+
+ if (projectResponse.ok) {
+ return await projectResponse.json();
+ }
+
+ // In the case where the status code is not 2xx,
+ // we don't know what to do with the response other than just show an error message.
+ const message = await projectResponse.text();
+ throw new Error(message.slice(0, 1000));
};