2024-05-26 17:13:34 +02:00
|
|
|
import { useMemo } from "react";
|
|
|
|
|
import { IconExclamationCircle } from "@tabler/icons-react";
|
|
|
|
|
import { TRPCClientError } from "@trpc/client";
|
|
|
|
|
import type { DefaultErrorData } from "@trpc/server/unstable-core-do-not-import";
|
|
|
|
|
|
|
|
|
|
import type { WidgetKind } from "@homarr/definitions";
|
|
|
|
|
|
2024-09-17 19:30:14 +02:00
|
|
|
import type { WidgetDefinition } from "..";
|
2024-05-26 17:13:34 +02:00
|
|
|
import { widgetImports } from "..";
|
|
|
|
|
import { ErrorBoundaryError } from "./base";
|
|
|
|
|
import { BaseWidgetError } from "./base-component";
|
|
|
|
|
|
|
|
|
|
interface WidgetErrorProps {
|
|
|
|
|
kind: WidgetKind;
|
|
|
|
|
error: unknown;
|
|
|
|
|
resetErrorBoundary: () => void;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export const WidgetError = ({ error, resetErrorBoundary, kind }: WidgetErrorProps) => {
|
|
|
|
|
const currentDefinition = useMemo(() => widgetImports[kind].definition, [kind]);
|
|
|
|
|
|
|
|
|
|
if (error instanceof ErrorBoundaryError) {
|
|
|
|
|
return <BaseWidgetError {...error.getErrorBoundaryData()} onRetry={resetErrorBoundary} />;
|
|
|
|
|
}
|
|
|
|
|
|
2024-09-17 19:30:14 +02:00
|
|
|
const commonFallbackError = (
|
|
|
|
|
<BaseWidgetError
|
|
|
|
|
icon={IconExclamationCircle}
|
|
|
|
|
message={(error as { toString: () => string }).toString()}
|
|
|
|
|
onRetry={resetErrorBoundary}
|
|
|
|
|
/>
|
|
|
|
|
);
|
|
|
|
|
|
2024-05-26 17:13:34 +02:00
|
|
|
if (error instanceof TRPCClientError && "code" in error.data) {
|
|
|
|
|
const errorData = error.data as DefaultErrorData;
|
|
|
|
|
|
2024-09-17 19:30:14 +02:00
|
|
|
if (!("errors" in currentDefinition)) return commonFallbackError;
|
2024-05-26 17:13:34 +02:00
|
|
|
|
2024-09-17 19:30:14 +02:00
|
|
|
const errors: Exclude<WidgetDefinition["errors"], undefined> = currentDefinition.errors;
|
|
|
|
|
const errorDefinition = errors[errorData.code];
|
2024-05-26 17:13:34 +02:00
|
|
|
|
2024-09-17 19:30:14 +02:00
|
|
|
if (!errorDefinition) return commonFallbackError;
|
|
|
|
|
|
|
|
|
|
return (
|
|
|
|
|
<BaseWidgetError {...errorDefinition} onRetry={resetErrorBoundary} showLogsLink={!errorDefinition.hideLogsLink} />
|
|
|
|
|
);
|
2024-05-26 17:13:34 +02:00
|
|
|
}
|
|
|
|
|
|
2024-09-17 19:30:14 +02:00
|
|
|
return commonFallbackError;
|
2024-05-26 17:13:34 +02:00
|
|
|
};
|