Skip to content

Commit

Permalink
✨ add try-catch statement to handle errors
Browse files Browse the repository at this point in the history
  • Loading branch information
JohnsonMao committed Sep 27, 2023
1 parent 13320b9 commit 885de38
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 43 deletions.
16 changes: 4 additions & 12 deletions src/app/[lang]/posts/[postId]/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -21,27 +21,19 @@ export async function generateStaticParams() {
export async function generateMetadata({
params: { postId },
}: PostParams): Promise<Metadata> {
const posts = await getAllDataFrontmatter('posts');
const post = posts.find((post) => post.id === postId);
const post = await getDataById('posts', postId);

if (!post) return notFound();

return {
title: post.title,
description: post.excerpt,
};
return post.frontmatter;
}

async function PostPage({ params: { postId } }: PostParams) {
const posts = await getAllDataFrontmatter('posts');
const post = posts.find((post) => post.id === postId);
const post = await getDataById('posts', postId);

if (!post) return notFound();

const { content, frontmatter, source } = await getDataById(
'posts',
postId
);
const { content, frontmatter, source } = post;
const formattedDate = formatDate(frontmatter.date);

return (
Expand Down
11 changes: 11 additions & 0 deletions src/utils/__tests__/mdx.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,17 @@ describe('Get post data function', () => {
source: '---\ndate: 2023/07/09\n---\n\n測試文章C',
});
});

it('should return null when trying to get post data for a non-existing file', async () => {
mockExists.mockReturnValueOnce(false);
mockReadFile.mockImplementation(() => {
throw new Error('File not found');
});

const postData = await getDataById('posts', 'not_found');

expect(postData).toBe(null);
});
});

const mockFileA = `---
Expand Down
66 changes: 35 additions & 31 deletions src/utils/mdx.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,37 +45,41 @@ export async function getAllDataFrontmatter(dirType: DataDirType) {

/** Retrieve data content and front matter for a specific data file by its id. */
export async function getDataById(dirType: DataDirType, id: string) {
const dirPath = path.join(ROOT_PATH, 'data', dirType);
const mdxPath = path.join(dirPath, `${id}.mdx`);
const mdPath = path.join(dirPath, `${id}.md`);
const fullPath = fs.existsSync(mdxPath) ? mdxPath : mdPath;
const source = fs.readFileSync(fullPath, 'utf8');
const { content, frontmatter } = await compileMDX<DataFrontmatter>({
source,
components: {
h1: H1,
h2: H2,
h3: H3,
h4: H4,
h5: H5,
h6: H6,
pre: CodeBox,
img: Image as () => JSX.Element,
a: Link as () => JSX.Element,
},
options: {
parseFrontmatter: true,
mdxOptions: {
remarkPlugins: [remarkGfm],
rehypePlugins: [
rehypeSlug,
rehypeCodeTitles,
rehypePrismPlus,
rehypeImageMetadata,
],
try {
const dirPath = path.join(ROOT_PATH, 'data', dirType);
const mdxPath = path.join(dirPath, `${id}.mdx`);
const mdPath = path.join(dirPath, `${id}.md`);
const fullPath = fs.existsSync(mdxPath) ? mdxPath : mdPath;
const source = fs.readFileSync(fullPath, 'utf8');
const { content, frontmatter } = await compileMDX<DataFrontmatter>({
source,
components: {
h1: H1,
h2: H2,
h3: H3,
h4: H4,
h5: H5,
h6: H6,
pre: CodeBox,
img: Image as () => JSX.Element,
a: Link as () => JSX.Element,
},
options: {
parseFrontmatter: true,
mdxOptions: {
remarkPlugins: [remarkGfm],
rehypePlugins: [
rehypeSlug,
rehypeCodeTitles,
rehypePrismPlus,
rehypeImageMetadata,
],
},
},
},
});
});

return { id, content, frontmatter, source };
return { id, content, frontmatter, source };
} catch {
return null;
}
}

0 comments on commit 885de38

Please sign in to comment.