-
Notifications
You must be signed in to change notification settings - Fork 593
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Refactor code to add ErrorBoundary component for better error handling
- Loading branch information
1 parent
1c356e0
commit 7e8660c
Showing
5 changed files
with
66 additions
and
44 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
import React from "react"; | ||
|
||
class ErrorBoundary extends React.Component { | ||
constructor(props) { | ||
super(props); | ||
this.state = { hasError: false }; | ||
} | ||
|
||
static getDerivedStateFromError(error) { | ||
// Update state so the next render shows the fallback UI | ||
return { hasError: true }; | ||
} | ||
|
||
componentDidCatch(error, errorInfo) { | ||
// You can also log the error to an error reporting service | ||
console.error("ErrorBoundary caught an error:", error, errorInfo); | ||
} | ||
|
||
render() { | ||
if (this.state.hasError) { | ||
return ( | ||
<div className="flex h-screen items-center justify-center"> | ||
<h1 className="text-red-600">Something went wrong.</h1> | ||
</div> | ||
); | ||
} | ||
|
||
return this.props.children; | ||
} | ||
} | ||
|
||
export default ErrorBoundary; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,12 +1,15 @@ | ||
import "@/styles/globals.css"; | ||
import { Inter } from "next/font/google"; | ||
import ErrorBoundary from "@/components/ErrorBoundary"; | ||
|
||
const inter = Inter({ subsets: ["latin"] }); | ||
|
||
export default function App({ Component, pageProps }) { | ||
return ( | ||
<main className={inter.className}> | ||
<Component {...pageProps} /> | ||
<ErrorBoundary> | ||
<Component {...pageProps} /> | ||
</ErrorBoundary> | ||
</main> | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.