Skip to content

Commit

Permalink
Errors
Browse files Browse the repository at this point in the history
  • Loading branch information
istarkov committed Sep 19, 2024
1 parent f128c09 commit 13f6b07
Show file tree
Hide file tree
Showing 4 changed files with 84 additions and 79 deletions.
12 changes: 4 additions & 8 deletions apps/builder/app/routes/rest.build.$buildId.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import { getUserById, type User } from "~/shared/db/user.server";
import { preventCrossOriginCookie } from "~/services/no-cross-origin-cookie";
import { allowedDestinations } from "~/services/destinations.server";
import { isDashboard } from "~/shared/router-utils";
import { parseError } from "~/shared/error/error-parse";

export const loader = async ({
params,
Expand Down Expand Up @@ -71,13 +72,8 @@ export const loader = async ({
console.error({ error });

// We have no idea what happened, so we'll return a 500 error.
return json(
error instanceof Error
? { error: "rest.buildId error", message: error.message }
: { error: "rest.buildId error", message: String(error) },
{
status: 500,
}
);
throw json(parseError(error), {
status: 500,
});
}
};
14 changes: 4 additions & 10 deletions apps/builder/app/routes/rest.buildId.$projectId.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import { db as projectDb } from "@webstudio-is/project/index.server";
import { allowedDestinations } from "~/services/destinations.server";
import { preventCrossOriginCookie } from "~/services/no-cross-origin-cookie";
import { createContext } from "~/shared/context.server";
import { parseError } from "~/shared/error/error-parse";
import { isDashboard } from "~/shared/router-utils";

// This loader is only accessible from the dashboard origin
Expand Down Expand Up @@ -52,15 +53,8 @@ export const loader = async ({
throw error;
}

console.error({ error });

return json(
error instanceof Error
? { error: "rest.buildId error", message: error.message }
: { error: "rest.buildId error", message: String(error) },
{
status: 500,
}
);
throw json(parseError(error), {
status: 500,
});
}
};
64 changes: 3 additions & 61 deletions apps/builder/app/shared/error/error-boundary.tsx
Original file line number Diff line number Diff line change
@@ -1,75 +1,17 @@
import { isRouteErrorResponse, useRouteError } from "@remix-run/react";
import { useRouteError } from "@remix-run/react";
import { ClientOnly } from "../client-only";
import { lazy } from "react";
import { z } from "zod";
import { parseError } from "./error-parse";

const ErrorMessage = lazy(async () => {
const { ErrorMessage } = await import("./error-message.client");
return { default: ErrorMessage };
});

const PageError = z.union([
z.string().transform((message) => ({ message, description: undefined })),
z.object({
message: z.string(),
description: z.string().optional(),
}),
]);

const parseErrorObject = (
error: unknown
): {
status: number;
statusText?: string;
message: string;
description?: string;
} => {
if (error instanceof Error) {
return {
message: error.message,
status: 500,
};
}

if (isRouteErrorResponse(error)) {
const parsed = PageError.safeParse(error.data);

if (parsed.success) {
return {
message: parsed.data.message,
description: parsed.data.description,
status: error.status,
statusText: error.statusText,
};
}

return {
message: error.data ? JSON.stringify(error.data) : "unknown error",
status: error.status,
statusText: error.statusText,
};
}

const parsed = PageError.safeParse(error);
if (parsed.success) {
return {
message: parsed.data.message,
description: parsed.data.description,
status: 1001,
};
}

return {
message: JSON.stringify(error ?? "unknown error"),
status: 1001,
statusText: undefined,
};
};

export const ErrorBoundary = () => {
const rawError = useRouteError();

const error = parseErrorObject(rawError);
const error = parseError(rawError);

return (
<ClientOnly>
Expand Down
73 changes: 73 additions & 0 deletions apps/builder/app/shared/error/error-parse.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
import { isRouteErrorResponse } from "@remix-run/react";
import { z } from "zod";

// Currently, we have multiple error formats, and not all of them are covered here.
// We should consolidate these formats into a single, unified format for consistency.
const PageError = z.union([
z.string().transform((message) => ({ message, description: undefined })),
z
.object({
message: z.string(),
details: z.string(),
hint: z.string(),
code: z.string(),
})
.transform(({ message, details, hint, code }) => ({
message,
description: `${details} ${hint} ${code}`,
})),
z.object({
message: z.string(),
description: z.string().optional(),
}),
]);

export const parseError = (
error: unknown
): {
status: number;
statusText?: string;
message: string;
description?: string;
} => {
if (error instanceof Error) {
return {
message: error.message,
status: 500,
};
}

if (isRouteErrorResponse(error)) {
const parsed = PageError.safeParse(error.data);

if (parsed.success) {
return {
message: parsed.data.message,
description: parsed.data.description,
status: error.status,
statusText: error.statusText,
};
}

return {
message: error.data ? JSON.stringify(error.data) : "unknown error",
status: error.status,
statusText: error.statusText,
};
}

const parsed = PageError.safeParse(error);
if (parsed.success) {
return {
message: parsed.data.message,
description: parsed.data.description,
status: 1001,
};
}

return {
message: JSON.stringify(error ?? "unknown error"),
status: 1001,
statusText: undefined,
};
};

0 comments on commit 13f6b07

Please sign in to comment.