"use client";

import { ErrorBoundary } from "react-error-boundary";
import { Error as ErrorComp } from "./Error";

export const CustomErrorBoundary = ({ children }: { children?: React.ReactNode }) => {
	return (
		<ErrorBoundary
			fallbackRender={({ error }) => {
				console.log(error);
				let errorTest;
				let errorCode = 500;
				if ("statusCode" in error) {
					errorCode = error.statusCode;
				}
				if ("message" in error || error instanceof Error) {
					errorTest = error.message;
				} else if (typeof error === "string") {
					errorTest = error;
				} else {
					errorTest = "Ein unerwarteter Fehler ist aufgetreten.";
				}
				return <ErrorComp title={errorTest} statusCode={errorCode} />;
			}}
		>
			{children}
		</ErrorBoundary>
	);
};
