Skip to content
This repository was archived by the owner on Jun 28, 2025. It is now read-only.

Commit 257f9f7

Browse files
author
Manuel Proß
committed
feat(web): add error handling
1 parent 58efcaf commit 257f9f7

File tree

1 file changed

+38
-13
lines changed

1 file changed

+38
-13
lines changed

web/src/helpers/getPageContent.ts

Lines changed: 38 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -12,19 +12,44 @@ export async function getPageContent(slug: string[]) {
1212
},
1313
};
1414
const query = qs.stringify(params, { addQueryPrefix: true });
15-
const respose = await fetch(`${process.env.CMS_API}/menus/1${query}`, {
16-
// headers: {
17-
// authorization: `Bearer ${process.env.CMS_TOKEN}`,
18-
// },
19-
});
20-
const data = (await respose.json()).data;
21-
const page = data.attributes.items.data.find((item: { attributes: { url: string } }) => item.attributes.url.endsWith(slug[0]));
22-
const pageRelation = page?.attributes?.page_relation.data;
23-
24-
let pageContent;
25-
if (pageRelation) {
15+
let response, data, page, pageRelation, pageContent;
16+
17+
try {
18+
response = await fetch(`${process.env.CMS_API}/menus/1${query}`, {
19+
// headers: {
20+
// authorization: `Bearer ${process.env.CMS_TOKEN}`,
21+
// },
22+
});
23+
24+
if (!response.ok) {
25+
throw new Error(`Failed to fetch menu data: ${response.statusText}`);
26+
}
27+
28+
data = await response.json();
29+
} catch (error) {
30+
console.error("Error fetching menu data:", error);
31+
return { error: "Failed to fetch menu data" };
32+
}
33+
34+
try {
35+
page = data.data.attributes.items.data.find((item: { attributes: { url: string } }) => item.attributes.url.endsWith(slug[0]));
36+
pageRelation = page?.attributes?.page_relation?.data;
37+
38+
if (!pageRelation) {
39+
throw new Error("Page relation not found");
40+
}
41+
2642
const pageRes = await fetch(`${process.env.CMS_API}/pages/${pageRelation.id}?populate=*`);
27-
pageContent = (await pageRes.json()).data;
43+
44+
if (!pageRes.ok) {
45+
throw new Error(`Failed to fetch page data: ${pageRes.statusText}`);
46+
}
47+
48+
pageContent = await pageRes.json();
49+
} catch (error) {
50+
console.error("Error fetching page data:", error);
51+
return { error: "Failed to fetch page data" };
2852
}
29-
return { page: pageContent };
53+
54+
return { page: pageContent.data };
3055
}

0 commit comments

Comments
 (0)