From d32afe310d34b76d43b02d95a1bfb4ee37c18a48 Mon Sep 17 00:00:00 2001 From: Hazem Krimi Date: Sun, 13 Feb 2022 19:37:57 +0100 Subject: [PATCH] Handle file reading errors --- utils/blog.ts | 32 ++++++++++++++++++++------------ utils/portfolio.ts | 32 ++++++++++++++++++++------------ 2 files changed, 40 insertions(+), 24 deletions(-) diff --git a/utils/blog.ts b/utils/blog.ts index 15238f9..a2cbe01 100644 --- a/utils/blog.ts +++ b/utils/blog.ts @@ -5,9 +5,11 @@ import matter from 'gray-matter'; const blogPostsDirectory = path.join(process.cwd(), '_blog'); export const getBlogPosts = () => { - const fileNames = fs.readdirSync(blogPostsDirectory); + try { + const fileNames = fs.readdirSync(blogPostsDirectory); + + if (!fileNames) return []; - if (fileNames) { const allBlogPostsData = fileNames.map(filename => { const slug = filename.replace('.mdx', ''); @@ -36,15 +38,17 @@ export const getBlogPosts = () => { return -1; } }); + } catch { + return []; } - - return []; }; export const getBlogPostsSlugs = () => { - const fileNames = fs.readdirSync(blogPostsDirectory); + try { + const fileNames = fs.readdirSync(blogPostsDirectory); + + if (!fileNames) return []; - if (fileNames) { return fileNames.map(filename => { return { params: { @@ -52,16 +56,20 @@ export const getBlogPostsSlugs = () => { } }; }); + } catch { + return []; } - - return []; }; export const getBlogPostdata = async (slug: string) => { - const fullPath = path.join(blogPostsDirectory, `${slug}.mdx`); - const postContent = fs.readFileSync(fullPath, 'utf8'); + try { + const fullPath = path.join(blogPostsDirectory, `${slug}.mdx`); + const postContent = fs.readFileSync(fullPath, 'utf8'); - if (!postContent) return undefined; + if (!postContent) return undefined; - return postContent; + return postContent; + } catch { + return undefined; + } }; diff --git a/utils/portfolio.ts b/utils/portfolio.ts index 36d1be8..3e7395b 100644 --- a/utils/portfolio.ts +++ b/utils/portfolio.ts @@ -5,9 +5,11 @@ import matter from 'gray-matter'; const portfolioProjects = path.join(process.cwd(), '_portfolio'); export const getPortfolioProjects = () => { - const fileNames = fs.readdirSync(portfolioProjects); + try { + const fileNames = fs.readdirSync(portfolioProjects); + + if (!fileNames) return []; - if (fileNames) { const allPortfolioProjectsData = fileNames.map(filename => { const slug = filename.replace('.mdx', ''); @@ -36,15 +38,17 @@ export const getPortfolioProjects = () => { return -1; } }); + } catch { + return []; } - - return []; }; export const getPortfolioPorjectsSlugs = () => { - const fileNames = fs.readdirSync(portfolioProjects); + try { + const fileNames = fs.readdirSync(portfolioProjects); + + if (!fileNames) return []; - if (fileNames) { return fileNames.map(filename => { return { params: { @@ -52,16 +56,20 @@ export const getPortfolioPorjectsSlugs = () => { } }; }); + } catch { + return []; } - - return []; }; export const getPortfolioProjectdata = async (slug: string) => { - const fullPath = path.join(portfolioProjects, `${slug}.mdx`); - const postContent = fs.readFileSync(fullPath, 'utf8'); + try { + const fullPath = path.join(portfolioProjects, `${slug}.mdx`); + const postContent = fs.readFileSync(fullPath, 'utf8'); - if (!postContent) return undefined; + if (!postContent) return undefined; - return postContent; + return postContent; + } catch { + return undefined; + } };