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
+14 -6
View File
@@ -5,9 +5,11 @@ import matter from 'gray-matter';
const blogPostsDirectory = path.join(process.cwd(), '_blog');
export const getBlogPosts = () => {
try {
const fileNames = fs.readdirSync(blogPostsDirectory);
if (fileNames) {
if (!fileNames) return [];
const allBlogPostsData = fileNames.map(filename => {
const slug = filename.replace('.mdx', '');
@@ -36,15 +38,17 @@ export const getBlogPosts = () => {
return -1;
}
});
}
} catch {
return [];
}
};
export const getBlogPostsSlugs = () => {
try {
const fileNames = fs.readdirSync(blogPostsDirectory);
if (fileNames) {
if (!fileNames) return [];
return fileNames.map(filename => {
return {
params: {
@@ -52,16 +56,20 @@ export const getBlogPostsSlugs = () => {
}
};
});
}
} catch {
return [];
}
};
export const getBlogPostdata = async (slug: string) => {
try {
const fullPath = path.join(blogPostsDirectory, `${slug}.mdx`);
const postContent = fs.readFileSync(fullPath, 'utf8');
if (!postContent) return undefined;
return postContent;
} catch {
return undefined;
}
};
+14 -6
View File
@@ -5,9 +5,11 @@ import matter from 'gray-matter';
const portfolioProjects = path.join(process.cwd(), '_portfolio');
export const getPortfolioProjects = () => {
try {
const fileNames = fs.readdirSync(portfolioProjects);
if (fileNames) {
if (!fileNames) return [];
const allPortfolioProjectsData = fileNames.map(filename => {
const slug = filename.replace('.mdx', '');
@@ -36,15 +38,17 @@ export const getPortfolioProjects = () => {
return -1;
}
});
}
} catch {
return [];
}
};
export const getPortfolioPorjectsSlugs = () => {
try {
const fileNames = fs.readdirSync(portfolioProjects);
if (fileNames) {
if (!fileNames) return [];
return fileNames.map(filename => {
return {
params: {
@@ -52,16 +56,20 @@ export const getPortfolioPorjectsSlugs = () => {
}
};
});
}
} catch {
return [];
}
};
export const getPortfolioProjectdata = async (slug: string) => {
try {
const fullPath = path.join(portfolioProjects, `${slug}.mdx`);
const postContent = fs.readFileSync(fullPath, 'utf8');
if (!postContent) return undefined;
return postContent;
} catch {
return undefined;
}
};