diff --git a/src/components/ErrorBoundary.tsx b/src/components/ErrorBoundary.tsx new file mode 100644 index 00000000000..25c42fb9119 --- /dev/null +++ b/src/components/ErrorBoundary.tsx @@ -0,0 +1,38 @@ +"use client"; +import { Component, ReactNode } from "react"; + +type Props = { + children: ReactNode; +}; + +type State = { + hasError: boolean; +}; + +export class ErrorBoundary extends Component { + constructor(props: Props) { + super(props); + this.state = { hasError: false }; + } + + static getDerivedStateFromError(_: Error) { + return { hasError: true }; + } + + componentDidCatch(error: Error, info: any) { + console.error("Caught by ErrorBoundary:", error, info); + } + + render() { + if (this.state.hasError) { + return ( +
+

Something went wrong.

+

Try refreshing the page. If the problem persists, please report it.

+
+ ); + } + + return this.props.children; + } + } diff --git a/src/pages/_app.tsx b/src/pages/_app.tsx index 5431f87cc9e..7deeac26b77 100644 --- a/src/pages/_app.tsx +++ b/src/pages/_app.tsx @@ -1,6 +1,7 @@ /* * Copyright (c) Facebook, Inc. and its affiliates. */ +import { ErrorBoundary } from '../components/ErrorBoundary'; import {useEffect} from 'react'; import {AppProps} from 'next/app'; @@ -54,5 +55,7 @@ export default function MyApp({Component, pageProps}: AppProps) { }; }, [router.events]); - return ; + return + + ; }