Next.js 13+ How to test global-error.tsx during development
Asked Answered
B

1

10

I added global-error.tsx file to the Next.js project v 13.4.16. I am wondering how I can simulate it to see how this looks like in UI during development.

I keep my global-error.tsx file on the same level as root layout.tsx file in app/[locale] directory (since I am using next-intl).

Is there any way to test it?

I have tried throwing an error in root layout file, but it seems to not catch this error. Instead of global-error page, I see Next.js error indicator.

enter image description here

Here is how my global-error looks like:

"use client";
import * as React from "react";
import { defaultLanguage } from "@/app/intl";
import { Layout } from "@/app/[locale]/home/HeroSection/Layout";
import { Navbar } from "@/app/[locale]/components/Navbar";
import Footer from "@/app/[locale]/components/Footer/Footer";
import { Box } from "@mui/material";
import Link from "next/link";
import { Button } from "@/app/[locale]/components/Button";
import Paragraph from "@/app/[locale]/components/ResponsiveText/Paragraph";

type Props = {
  error: Error & { digest?: string };
  reset: () => void;
};
const GlobalError = ({ error, reset }: Props) => {
  return (
    <html lang={defaultLanguage}>
      <body>
        <Navbar lng={defaultLanguage} />
        <Box minHeight={"calc(100vh - 248px)"}>
          <Paragraph>Something went wrong...</Paragraph>
          <Button
            href="/"
            version={"dark"}
            LinkComponent={Link}
            variant="outlined"
          >
            Go back
          </Button>
        </Box>

        <Footer />
      </body>
    </html>
  );
};

export default GlobalError;

And here is how I am trying to test it in root layout:

export default async function LocaleLayout({
  children,
  params: { locale },
}: LocaleLayout) {
  let messages;
  try {
    messages = (await import(`../../messages/${locale}.json`)).default;
  } catch (error) {
    notFound();
  }

  throw new Error("test");
  return (
    <html lang={locale}>
      <body style={{ backgroundColor: "#695E48" }}>
        <NextIntlClientProvider locale={locale} messages={messages}>
          <ThemeRegistry options={{ key: "mui" }}>
            {children}
            <Footer />
            <Analytics />
          </ThemeRegistry>
        </NextIntlClientProvider>
      </body>
    </html>
  );
}
Blastomere answered 14/12, 2023 at 18:43 Comment(0)
O
1

From the docs here:

Good to know:

global-error.js is only enabled in production. In development, our error overlay will show instead.

and here:

While designing error UI, you may find it helpful to use the React Developer Tools to manually toggle Error boundaries.

So:

  1. install React Developer Tools
  2. open your browser Developer Tools, and open the new 'Component' tab
  3. select the component inside 'Error Boundary' component, and click this (!) icon enter image description here
Obliging answered 26/6, 2024 at 9:14 Comment(0)

© 2022 - 2025 — McMap. All rights reserved.