Files
Ivan Starkov f653a116e7 chore: Update fixtures date (#4138)
## Description

1. What is this PR about (link the issue and add a short description)

## Steps for reproduction

1. click button
2. expect xyz

## Code Review

- [ ] hi @kof, I need you to do
  - conceptual review (architecture, feature-correctness)
  - detailed review (read every line)
  - test it on preview

## Before requesting a review

- [ ] made a self-review
- [ ] added inline comments where things may be not obvious (the "why",
not "what")

## Before merging

- [ ] tested locally and on preview environment (preview dev login:
5de6)
- [ ] updated [test
cases](https://github.com/webstudio-is/webstudio/blob/main/apps/builder/docs/test-cases.md)
document
- [ ] added tests
- [ ] if any new env variables are added, added them to `.env` file
2024-09-19 23:08:58 +03:00

74 lines
1.7 KiB
TypeScript

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().nullable(),
code: z.string(),
})
.transform(({ message, details, hint, code }) => ({
message,
description: `details: ${details}; hint: ${hint}; code: ${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,
};
};