import { AlertIcon } from '@primer/octicons-react'; import { isRouteErrorResponse, useRouteError } from 'react-router'; import { cn } from '~/utils/cn'; import Card from './Card'; import Code from './Code'; interface Props { type?: 'full' | 'embedded'; } function getMessage(error: Error | unknown) { if (!(error instanceof Error)) { return "An unknown error occurred"; } let rootError = error; // Traverse the error chain to find the root cause if (error.cause) { rootError = error.cause as Error; while (rootError.cause) { rootError = rootError.cause as Error; } } // If we are aggregate, concat into a single message if (rootError instanceof AggregateError) { return rootError.errors.map((error) => error.message).join('\n'); } return rootError.message; } export function ErrorPopup({ type = 'full' }: Props) { const error = useRouteError(); const routing = isRouteErrorResponse(error); const message = getMessage(error); return (
{routing ? error.status : 'Error'}
{routing ? error.statusText : {message}}
); }