DEV Community

Ogasawara Kakeru
Ogasawara Kakeru

Posted on

Handling Uncaught Exceptions with Global errors in Next.js

While less common, you can handle errors in the root layout using the global-error.js file, located in the root app directory, even when leveraging internationalization. Global error UI must define its own and tags, since it is replacing the root layout or template when active.

app/global-error.tsx

'use client' // Error boundaries must be Client Components
export default function GlobalError({
  error,
  retry,
}: {
  error: unknown
  retry: () => void
}) {
  return (
    // global-error must include html and body tags
    <html>
      <body>
        <h2>Something went wrong!</h2>
        <button onClick={() => retry()}>Try again</button>
      </body>
    </html>
  )
}
Enter fullscreen mode Exit fullscreen mode

Top comments (0)