Handle file reading errors

This commit is contained in:
Hazem Krimi
2022-02-13 19:37:57 +01:00
parent 8fdc27152a
commit d32afe310d
2 changed files with 40 additions and 24 deletions
+20 -12
View File
@@ -5,9 +5,11 @@ import matter from 'gray-matter';
const blogPostsDirectory = path.join(process.cwd(), '_blog'); const blogPostsDirectory = path.join(process.cwd(), '_blog');
export const getBlogPosts = () => { 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 allBlogPostsData = fileNames.map(filename => {
const slug = filename.replace('.mdx', ''); const slug = filename.replace('.mdx', '');
@@ -36,15 +38,17 @@ export const getBlogPosts = () => {
return -1; return -1;
} }
}); });
} catch {
return [];
} }
return [];
}; };
export const getBlogPostsSlugs = () => { export const getBlogPostsSlugs = () => {
const fileNames = fs.readdirSync(blogPostsDirectory); try {
const fileNames = fs.readdirSync(blogPostsDirectory);
if (!fileNames) return [];
if (fileNames) {
return fileNames.map(filename => { return fileNames.map(filename => {
return { return {
params: { params: {
@@ -52,16 +56,20 @@ export const getBlogPostsSlugs = () => {
} }
}; };
}); });
} catch {
return [];
} }
return [];
}; };
export const getBlogPostdata = async (slug: string) => { export const getBlogPostdata = async (slug: string) => {
const fullPath = path.join(blogPostsDirectory, `${slug}.mdx`); try {
const postContent = fs.readFileSync(fullPath, 'utf8'); 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;
}
}; };
+20 -12
View File
@@ -5,9 +5,11 @@ import matter from 'gray-matter';
const portfolioProjects = path.join(process.cwd(), '_portfolio'); const portfolioProjects = path.join(process.cwd(), '_portfolio');
export const getPortfolioProjects = () => { 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 allPortfolioProjectsData = fileNames.map(filename => {
const slug = filename.replace('.mdx', ''); const slug = filename.replace('.mdx', '');
@@ -36,15 +38,17 @@ export const getPortfolioProjects = () => {
return -1; return -1;
} }
}); });
} catch {
return [];
} }
return [];
}; };
export const getPortfolioPorjectsSlugs = () => { export const getPortfolioPorjectsSlugs = () => {
const fileNames = fs.readdirSync(portfolioProjects); try {
const fileNames = fs.readdirSync(portfolioProjects);
if (!fileNames) return [];
if (fileNames) {
return fileNames.map(filename => { return fileNames.map(filename => {
return { return {
params: { params: {
@@ -52,16 +56,20 @@ export const getPortfolioPorjectsSlugs = () => {
} }
}; };
}); });
} catch {
return [];
} }
return [];
}; };
export const getPortfolioProjectdata = async (slug: string) => { export const getPortfolioProjectdata = async (slug: string) => {
const fullPath = path.join(portfolioProjects, `${slug}.mdx`); try {
const postContent = fs.readFileSync(fullPath, 'utf8'); 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;
}
}; };