Skip to content

Commit cbba2b2

Browse files
committed
feat: added trycatch in api route. triggering deployment
1 parent 2cac7bd commit cbba2b2

File tree

1 file changed

+35
-27
lines changed

1 file changed

+35
-27
lines changed

src/pages/api/readfiles.ts

Lines changed: 35 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -5,34 +5,42 @@ import { type NextApiRequest, type NextApiResponse } from "next";
55
import path from "path";
66

77
const handler = (req: NextApiRequest, res: NextApiResponse) => {
8-
const __next__base__dirname = __dirname.split(".next")[0] as string;
9-
let contentDir = path.join(CONTENT_PATH);
10-
if (process.env.NODE_ENV === "production") {
11-
contentDir = path.join(__next__base__dirname, CONTENT_PATH);
12-
}
13-
const directories = fs.readdirSync(path.join(contentDir));
14-
const lessons: object[] = [];
15-
directories.reverse().map((folder) => {
16-
if (fs.lstatSync(path.join(contentDir, folder)).isDirectory()) {
17-
fs.readdirSync(path.join(contentDir, folder)).map((file) => {
18-
if (!fs.lstatSync(path.join(contentDir, folder, file)).isDirectory()) {
19-
const markdownWithMeta = fs.readFileSync(
20-
path.join(contentDir, folder, file),
21-
"utf-8",
22-
);
23-
24-
const { data: frontMatter } = matter(markdownWithMeta);
25-
lessons.push({
26-
path: folder,
27-
frontMatter,
28-
slug: `${file.replace(".mdx", "")}`,
29-
});
30-
}
31-
});
8+
try {
9+
const __next__base__dirname = __dirname.split(".next")[0] as string;
10+
let contentDir = path.join(CONTENT_PATH);
11+
if (process.env.NODE_ENV === "production") {
12+
contentDir = path.join(__next__base__dirname, CONTENT_PATH);
3213
}
33-
});
34-
res.statusCode = 200;
35-
res.json(lessons);
14+
const directories = fs.readdirSync(path.join(contentDir));
15+
const lessons: object[] = [];
16+
directories.reverse().map((folder) => {
17+
if (fs.lstatSync(path.join(contentDir, folder)).isDirectory()) {
18+
fs.readdirSync(path.join(contentDir, folder)).map((file) => {
19+
if (
20+
!fs.lstatSync(path.join(contentDir, folder, file)).isDirectory()
21+
) {
22+
const markdownWithMeta = fs.readFileSync(
23+
path.join(contentDir, folder, file),
24+
"utf-8",
25+
);
26+
27+
const { data: frontMatter } = matter(markdownWithMeta);
28+
lessons.push({
29+
path: folder,
30+
frontMatter,
31+
slug: `${file.replace(".mdx", "")}`,
32+
});
33+
}
34+
});
35+
}
36+
});
37+
res.statusCode = 200;
38+
res.json(lessons);
39+
} catch (error) {
40+
console.log(error);
41+
res.statusCode = 500;
42+
res.json({ error });
43+
}
3644
};
3745

3846
export default handler;

0 commit comments

Comments
 (0)