Skip to content

Commit 7e8660c

Browse files
committed
Refactor code to add ErrorBoundary component for better error handling
1 parent 1c356e0 commit 7e8660c

File tree

5 files changed

+66
-44
lines changed

5 files changed

+66
-44
lines changed

components/ErrorBoundary.jsx

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
import React from "react";
2+
3+
class ErrorBoundary extends React.Component {
4+
constructor(props) {
5+
super(props);
6+
this.state = { hasError: false };
7+
}
8+
9+
static getDerivedStateFromError(error) {
10+
// Update state so the next render shows the fallback UI
11+
return { hasError: true };
12+
}
13+
14+
componentDidCatch(error, errorInfo) {
15+
// You can also log the error to an error reporting service
16+
console.error("ErrorBoundary caught an error:", error, errorInfo);
17+
}
18+
19+
render() {
20+
if (this.state.hasError) {
21+
return (
22+
<div className="flex h-screen items-center justify-center">
23+
<h1 className="text-red-600">Something went wrong.</h1>
24+
</div>
25+
);
26+
}
27+
28+
return this.props.children;
29+
}
30+
}
31+
32+
export default ErrorBoundary;

components/ProfileList.json

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -455,6 +455,5 @@
455455
"Groot-2001.json",
456456
"kshashikumar.json",
457457
"Stoppedwumm.json",
458-
"vishal065.json",
459-
"berhili098.json"
458+
"vishal065.json"
460459
]

pages/_app.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,15 @@
11
import "@/styles/globals.css";
22
import { Inter } from "next/font/google";
3+
import ErrorBoundary from "@/components/ErrorBoundary";
34

45
const inter = Inter({ subsets: ["latin"] });
56

67
export default function App({ Component, pageProps }) {
78
return (
89
<main className={inter.className}>
9-
<Component {...pageProps} />
10+
<ErrorBoundary>
11+
<Component {...pageProps} />
12+
</ErrorBoundary>
1013
</main>
1114
);
1215
}

pages/index.js

Lines changed: 29 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -22,35 +22,38 @@ function App() {
2222
const router = useRouter();
2323
const currentUrl = router.pathname;
2424

25-
useEffect(() => {
26-
const fetchData = async (file) => {
27-
try {
28-
const response = await fetch(file);
29-
const data = await response.json();
30-
return data;
31-
} catch (error) {
32-
console.error("Error fetching data:", error);
33-
return [];
34-
}
35-
};
36-
37-
const combineData = async () => {
38-
setLoadingProfiles(true);
39-
try {
40-
const promises = filenames.map((file) => fetchData(`/data/${file}`));
41-
const combinedData = await Promise.all(promises).then((results) =>
42-
results.flat()
43-
);
44-
setCombinedData(combinedData);
45-
setShuffledProfiles(shuffleProfiles(combinedData));
46-
} catch (error) {
47-
console.error("Error combining data:", error);
48-
setCombinedData([]);
49-
setShuffledProfiles([]);
25+
const fetchData = async (file) => {
26+
try {
27+
const response = await fetch(file);
28+
if (!response.ok) {
29+
throw new Error(`HTTP error! status: ${response.status}`);
5030
}
31+
return await response.json();
32+
} catch (error) {
33+
console.error("Error fetching data:", error);
34+
return [];
35+
}
36+
};
37+
38+
const combineData = async () => {
39+
setLoadingProfiles(true);
40+
try {
41+
const promises = filenames.map((file) => fetchData(`/data/${file}`));
42+
const results = await Promise.all(promises);
43+
const combinedData = results.flat();
44+
setCombinedData(combinedData);
45+
setShuffledProfiles(shuffleProfiles(combinedData));
46+
} catch (error) {
47+
console.error("Error combining data:", error);
48+
setCombinedData([]);
49+
setShuffledProfiles([]);
50+
alert("Failed to load profiles. Please try again later.");
51+
} finally {
5152
setLoadingProfiles(false);
52-
};
53+
}
54+
};
5355

56+
useEffect(() => {
5457
combineData();
5558
}, []);
5659

public/data/berhili098.json

Lines changed: 0 additions & 15 deletions
This file was deleted.

0 commit comments

Comments
 (0)