Skip to content

Commit 9b1f026

Browse files
committed
Add Error Handling
1 parent 424bde3 commit 9b1f026

File tree

5 files changed

+81
-71
lines changed

5 files changed

+81
-71
lines changed

app/(home)/page.js

Lines changed: 38 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,16 @@ import GridContainer from '@/components/GridContainer';
77
import { unstable_noStore as noStore } from 'next/cache';
88
import Header from '@/components/Header';
99

10-
const sleep = ms => new Promise(resolve => setTimeout(resolve, ms));
11-
1210
export default async function Home() {
1311
noStore();
14-
await connectDb();
15-
const recenetProfiles = await RecentProfiles.find({}).sort({ updatedAt: 'desc' }).limit(8);
12+
let recenetProfiles = [];
13+
14+
try {
15+
await connectDb();
16+
recenetProfiles = await RecentProfiles.find({ }).sort({ updatedAt: 'desc' }).limit(8);
17+
} catch (error) {
18+
console.log('An error occurred in Home Page while fetching recent profiles');
19+
}
1620

1721
const formAction = async formData => {
1822
'use server';
@@ -25,7 +29,7 @@ export default async function Home() {
2529
<Header />
2630

2731
<main className="px-4">
28-
<div className="mx-auto max-w-screen-md text-center pt-[10vh]">
32+
<div className="mx-auto max-w-screen-md pt-[10vh] text-center">
2933
<h1 className="text-gradient text-4xl font-bold md:text-7xl">Git Glance</h1>
3034
<p className="text-gradient mb-16 mt-2 text-xl font-medium md:text-3xl ">
3135
Visualize Your GitHub Profile
@@ -34,57 +38,36 @@ export default async function Home() {
3438
<SearchBox formAction={formAction} />
3539
</div>
3640

37-
{/* <div className="mx-auto mt-28 max-w-screen-xl text-left md:mt-40">
38-
<h2 className="text-gradient text-xl font-semibold md:text-3xl">Recent Profiles</h2>
39-
40-
<div className="mt-4 grid grid-cols-1 gap-4 md:grid-cols-4">
41-
{recenetProfiles.map(profile => (
42-
<Link
43-
key={profile._id}
44-
href={`/${profile.username}`}
45-
className="box grid grid-cols-[3rem_1fr] items-center gap-5 text-left"
41+
{recenetProfiles.length > 0 && (
42+
<div className="mx-auto mt-32 max-w-screen-xl md:mt-36">
43+
<GridContainer
44+
className={'grid-cols-1 gap-3 md:grid-cols-4'}
45+
name={'Recent Profiles'}
46+
description={'Profiles that have been viewed recently'}
4647
>
47-
<img
48-
src={profile.avatarUrl}
49-
// src="https://github.com/devxprite.png"
50-
alt={profile.username}
51-
className="mx-auto size-12 rounded-full"
52-
/>
53-
<div>
54-
<p className="text-base font-semibold md:text-lg">{profile.name ?? profile.username}</p>
55-
<p className="text-sm text-gray-400 md:text-base">@{profile.username}</p>
56-
</div>
57-
</Link>
58-
))}
59-
</div>
60-
</div> */}
61-
<div className="mx-auto mt-32 max-w-screen-xl md:mt-36">
62-
<GridContainer
63-
className={'grid-cols-1 gap-3 md:grid-cols-4'}
64-
name={'Recent Profiles'}
65-
description={'Profiles that have been viewed recently'}
66-
>
67-
{recenetProfiles.map(profile => (
68-
<Link
69-
key={profile._id}
70-
href={`/${profile.username}`}
71-
className="box flex items-center gap-3 text-left md:gap-5"
72-
>
73-
<img
74-
src={profile.avatarUrl}
75-
alt={profile.username}
76-
className="size-10 grow-0 rounded-full md:size-12"
77-
/>
78-
<div>
79-
<p className="text-base font-semibold md:text-lg">
80-
{profile.name ?? profile.username}
81-
</p>
82-
<p className="-mt-1 text-sm text-gray-400 md:text-base">@{profile.username}</p>
83-
</div>
84-
</Link>
85-
))}
86-
</GridContainer>
87-
</div>
48+
{recenetProfiles.map(profile => (
49+
<Link
50+
key={profile._id}
51+
href={`/${profile.username}`}
52+
prefetch={false}
53+
className="box flex items-center gap-3 text-left md:gap-5"
54+
>
55+
<img
56+
src={profile.avatarUrl}
57+
alt={profile.username}
58+
className="size-10 grow-0 rounded-full md:size-12"
59+
/>
60+
<div>
61+
<p className="text-base font-semibold md:text-lg">
62+
{profile.name ?? profile.username}
63+
</p>
64+
<p className="-mt-1 text-sm text-gray-400 md:text-base">@{profile.username}</p>
65+
</div>
66+
</Link>
67+
))}
68+
</GridContainer>
69+
</div>
70+
)}
8871
</main>
8972
</>
9073
);

app/[username]/page.js

Lines changed: 25 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,15 @@ import RecentProfiles from '@/models/RecentProfiles';
1717
import connectDb from '@/lib/connectDb';
1818
import RateLimit from '@/components/RateLimit';
1919

20-
// meta data
2120
export const generateMetadata = async ({ params: { username } }) => {
2221
await connectDb();
2322
const userInfo = await fetchUserInfo(username);
23+
24+
if (!userInfo)
25+
return {
26+
title: 'User Not Found!',
27+
};
28+
2429
return {
2530
title: `${username} - GitHub Profile`,
2631
description: `${username} is a developer on GitHub with ${userInfo.followers.totalCount} followers and ${userInfo.repositories.totalCount} repositories.`,
@@ -31,24 +36,30 @@ export const generateMetadata = async ({ params: { username } }) => {
3136
};
3237

3338
const page = async ({ params: { username } }) => {
34-
await connectDb();
3539
console.log('Username:', username);
3640

3741
const userInfo = await fetchUserInfo(username);
3842
if (!userInfo) return notFound();
3943

40-
const updateRecentProfilesDb = async () =>
41-
await RecentProfiles.findOneAndUpdate(
42-
{ username },
43-
{
44-
name: userInfo.name ?? userInfo.username,
45-
username: userInfo.username,
46-
following: userInfo.following.totalCount,
47-
followers: userInfo.followers.totalCount,
48-
avatarUrl: userInfo.avatarUrl,
49-
},
50-
{ upsert: true, new: true, setDefaultsOnInsert: true },
51-
);
44+
45+
const updateRecentProfilesDb = async () => {
46+
try {
47+
await connectDb();
48+
await RecentProfiles.findOneAndUpdate(
49+
{ username },
50+
{
51+
name: userInfo.name ?? userInfo.username,
52+
username: userInfo.username,
53+
following: userInfo.following.totalCount,
54+
followers: userInfo.followers.totalCount,
55+
avatarUrl: userInfo.avatarUrl,
56+
},
57+
{ upsert: true, new: true, setDefaultsOnInsert: true },
58+
);
59+
} catch (error) {
60+
console.log('An error occurred while updating the recent profiles database:');
61+
}
62+
};
5263

5364
const [
5465
userActivity,

components/Header.js

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,20 @@ const Header = () => {
1010
{/* <img
1111
className="ml-auto h-9"
1212
src="https://api.producthunt.com/widgets/embed-image/v1/featured.svg?post_id=434915&theme=neutral" alt="" /> */}
13+
<a
14+
href="https://www.producthunt.com/posts/gitglance?utm_source=badge-featured&utm_medium=badge&utm_souce=badge-gitglance"
15+
target="_blank"
16+
className='ml-auto'
17+
>
18+
<img
19+
src="https://api.producthunt.com/widgets/embed-image/v1/featured.svg?post_id=450495&theme=neutral"
20+
alt="GitGlance - Tool&#0032;for&#0032;visualizing&#0032;a&#0032;developer&#0039;s&#0032;GitHub&#0032;profile | Product Hunt"
21+
// style="width: 250px; height: 54px;"
22+
// width="250"
23+
// height="54"
24+
className="h-9"
25+
/>
26+
</a>
1327
</div>
1428
</header>
1529
);

lib/connectDb.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,9 @@ async function connectDb() {
2020

2121
cached.promise = mongoose.connect(MONGODB_URI, opts).then(mongoose => {
2222
return mongoose;
23+
}).catch(error => {
24+
console.error('An error occurred while connecting to the database');
25+
return null;
2326
});
2427
}
2528
cached.conn = await cached.promise;

models/RecentProfiles.js

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,5 +11,4 @@ const RecentProfilesSchema = new mongoose.Schema(
1111
{ timestamps: true },
1212
);
1313

14-
export default mongoose.models.RecentProfiles || mongoose.model('RecentProfiles', RecentProfilesSchema);
15-
// export default mongoose.models.RecentProfiles
14+
export default mongoose.models.RecentProfiles || mongoose.model('RecentProfiles', RecentProfilesSchema);

0 commit comments

Comments
 (0)