'use client' import * as React from 'react' import { cn } from '../../lib/utils' export interface RouteError extends Error { digest?: string } export interface RouteErrorBoundaryProps { /** The error thrown during render — matches the Next.js `error.tsx` contract. */ error: RouteError /** Re-render the route segment — matches the Next.js `error.tsx` contract. */ reset: () => void /** Heading shown above the message. */ title?: string /** Body copy shown in production (the raw error is only shown in development). */ description?: string /** Label for the primary (retry) action. */ retryLabel?: string /** Label for the secondary (home) action. Hidden when `homeHref` is null. */ homeLabel?: string /** Destination for the secondary action. Pass `null` to hide it. Defaults to `/`. */ homeHref?: string | null /** * Render-prop override for the entire surface. Receives the error plus the * resolved `reset`/`goHome` handlers so apps can fully brand the page while * keeping the Next.js wiring. */ children?: (props: { error: RouteError reset: () => void goHome: () => void }) => React.ReactNode /** Called once on mount with the error — wire up your logger/Sentry here. */ onError?: (error: RouteError) => void /** Override the outer wrapper className. */ className?: string } /** * Route-level error surface for an app's `app/error.tsx`. * * Implements the Next.js App Router error contract (`{ error, reset }`) and * renders a neutral, brandable error card. Stack/digest details are only shown * in development. Use render-prop `children` for full custom UI. * * @example * // app/error.tsx * 'use client' * import { RouteErrorBoundary } from '@startsimpli/ui' * export default function Error(props: { error: Error & { digest?: string }; reset: () => void }) { * return * } */ export function RouteErrorBoundary({ error, reset, title = 'Something went wrong', description = 'We encountered an unexpected error. Please try again, or head back home if the problem persists.', retryLabel = 'Try again', homeLabel = 'Go home', homeHref = '/', children, onError, className, }: RouteErrorBoundaryProps) { React.useEffect(() => { onError?.(error) }, [error, onError]) const goHome = React.useCallback(() => { if (homeHref && typeof window !== 'undefined') { window.location.href = homeHref } }, [homeHref]) if (children) { return <>{children({ error, reset, goHome })} } const isDevelopment = process.env.NODE_ENV === 'development' return (

{title}

{description}

{isDevelopment && (

Error (development only)

{error.message}

{error.digest && (

Digest: {error.digest}

)} {error.stack && (
Stack trace
                  {error.stack}
                
)}
)} {!isDevelopment && error.digest && (

Reference:{' '} {error.digest}

)}
{homeHref && ( )}
) }