Skip to content

Commit 7d5d425

Browse files
committed
feat: add error handling on dynamic pages
1 parent 456b439 commit 7d5d425

File tree

5 files changed

+51
-24
lines changed

5 files changed

+51
-24
lines changed

src/app/collections/[handle]/page.tsx

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,18 @@ type Props = {
77

88
const BASEURL = process.env.NEXT_PUBLIC_BASE_URL ?? "http://localhost:3000"
99

10+
async function getCollection(handle: string) {
11+
const res = await fetch(`${BASEURL}/collections?handle=${handle}`)
12+
13+
if (!res.ok) {
14+
throw new Error(`Failed to fetch collection: ${handle}`)
15+
}
16+
17+
return res.json()
18+
}
19+
1020
export async function generateMetadata({ params }: Props): Promise<Metadata> {
11-
const { collection } = await fetch(
12-
`${BASEURL}/collections?handle=${params.handle}`
13-
).then((res) => res.json())
21+
const { collection } = await getCollection(params.handle)
1422

1523
return {
1624
title: `${collection.title} | Acme Store`,
@@ -19,9 +27,7 @@ export async function generateMetadata({ params }: Props): Promise<Metadata> {
1927
}
2028

2129
export default async function CollectionPage({ params }: Props) {
22-
const { collection } = await fetch(
23-
`${BASEURL}/collections?handle=${params.handle}`
24-
).then((res) => res.json())
30+
const { collection } = await getCollection(params.handle)
2531

2632
return <CollectionTemplate collection={collection} />
2733
}

src/app/order/confirmed/[id]/page.tsx

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,15 +6,23 @@ type Props = {
66
params: { id: string }
77
}
88

9+
async function getOrder(id: string) {
10+
const res = await medusaRequest("GET", `/orders/${id}`)
11+
12+
if (!res.ok) {
13+
throw new Error(`Failed to fetch order: ${id}`)
14+
}
15+
16+
return res.body
17+
}
18+
919
export const metadata: Metadata = {
1020
title: "Order Confirmed",
1121
description: "You purchase was successful",
1222
}
1323

1424
export default async function CollectionPage({ params }: Props) {
15-
const { order } = await medusaRequest("GET", `/orders/${params.id}`).then(
16-
(res) => res.body
17-
)
25+
const { order } = await getOrder(params.id)
1826

1927
return <OrderCompletedTemplate order={order} />
2028
}

src/app/order/details/[id]/page.tsx

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,18 @@ type Props = {
66
params: { id: string }
77
}
88

9+
async function getOrder(id: string) {
10+
const res = await medusaRequest("GET", `/orders/${id}`)
11+
12+
if (!res.ok) {
13+
throw new Error(`Failed to fetch order: ${id}`)
14+
}
15+
16+
return res.body
17+
}
18+
919
export async function generateMetadata({ params }: Props): Promise<Metadata> {
10-
const { order } = await medusaRequest("GET", `/orders/${params.id}`).then(
11-
(res) => res.body
12-
)
20+
const { order } = await getOrder(params.id)
1321

1422
return {
1523
title: `Order #${order.display_id}`,
@@ -18,9 +26,7 @@ export async function generateMetadata({ params }: Props): Promise<Metadata> {
1826
}
1927

2028
export default async function CollectionPage({ params }: Props) {
21-
const { order } = await medusaRequest("GET", `/orders/${params.id}`).then(
22-
(res) => res.body
23-
)
29+
const { order } = await getOrder(params.id)
2430

2531
return <OrderCompletedTemplate order={order} />
2632
}

src/app/products/[handle]/page.tsx

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,22 @@ type Props = {
66
params: { handle: string }
77
}
88

9-
export async function generateMetadata({ params }: Props): Promise<Metadata> {
10-
const { products } = await medusaRequest("GET", "/products", {
9+
async function getProducts(handle: string) {
10+
const res = await medusaRequest("GET", "/products", {
1111
query: {
12-
handle: params.handle,
12+
handle,
1313
},
14-
}).then((res) => res.body)
14+
})
15+
16+
if (!res.ok) {
17+
throw new Error(`Failed to fetch product: ${handle}`)
18+
}
19+
20+
return res.body
21+
}
22+
23+
export async function generateMetadata({ params }: Props): Promise<Metadata> {
24+
const { products } = await getProducts(params.handle)
1525

1626
const product = products[0]
1727

@@ -27,11 +37,7 @@ export async function generateMetadata({ params }: Props): Promise<Metadata> {
2737
}
2838

2939
export default async function CollectionPage({ params }: Props) {
30-
const { products } = await medusaRequest("GET", "/products", {
31-
query: {
32-
handle: params.handle,
33-
},
34-
}).then((res) => res.body)
40+
const { products } = await getProducts(params.handle)
3541

3642
return <ProductTemplate product={products[0]} />
3743
}

src/lib/medusa-fetch/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ export default async function medusaRequest(
4040

4141
return {
4242
status: result.status,
43+
ok: result.ok,
4344
body,
4445
}
4546
} catch (error: any) {

0 commit comments

Comments
 (0)