mirror of
https://github.com/hazemKrimi/personal-website.git
synced 2026-05-01 18:00:26 +00:00
Handle file reading errors
This commit is contained in:
+20
-12
@@ -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
@@ -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;
|
||||||
|
}
|
||||||
};
|
};
|
||||||
|
|||||||
Reference in New Issue
Block a user