Skip to content

Commit 859af0d

Browse files
committed
add error processing and 404 page
1 parent 7f6933f commit 859af0d

File tree

4 files changed

+63
-42
lines changed

4 files changed

+63
-42
lines changed

index.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ app.set('views', path.join(__dirname, 'server/views'));
1111

1212
app.use(express.static(path.join(__dirname, 'dist')));
1313
app.use('/', router);
14+
app.use('*', (req, res) => res.status(404).render('404'));
1415

1516
app.listen(PORT, HOSTNAME, () => {
1617
console.log(`Express app listening on ${HOSTNAME}:${PORT}`);

server/core/utils.js

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,7 @@ const repoPath = path.join(__dirname, '../../repo');
77
const options = { cwd: repoPath };
88

99
const runExec = async (command) => {
10-
const { stdout, stderr } = await exec(command, options);
11-
12-
if (stderr) {
13-
console.error(stderr);
14-
}
10+
const { stdout } = await exec(command, options);
1511

1612
return stdout;
1713
};

server/router.js

Lines changed: 54 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -10,62 +10,79 @@ const {
1010

1111
const router = new Router();
1212

13-
router.get('/', async (req, res) => res.render('index', {
14-
branches: await getBranches(),
15-
}));
13+
router.get('/', async (req, res) => {
14+
try {
15+
return res.render('index', {
16+
branches: await getBranches(),
17+
});
18+
} catch (error) {
19+
console.error(error);
20+
return res.status(404).render('404');
21+
}
22+
});
1623

1724
router.get('/:branch', async (req, res) => {
1825
const branch = req.params.branch;
1926
const file = req.query.file;
2027
const path = req.query.path;
2128

22-
if (file) {
23-
return res.render('preview', {
24-
branch,
25-
breadCrumbs: await getBreadCrumbs(branch, file),
26-
fileContent: await getFileContent(file),
27-
});
28-
}
29+
try {
30+
if (file) {
31+
return res.render('preview', {
32+
branch,
33+
breadCrumbs: await getBreadCrumbs(branch, file),
34+
fileContent: await getFileContent(file),
35+
});
36+
}
2937

30-
if (path) {
31-
return res.render('files', {
38+
if (path) {
39+
return res.render('files', {
40+
branch,
41+
files: await getFiles(path),
42+
breadCrumbs: await getBreadCrumbs(branch, path),
43+
});
44+
}
45+
46+
return res.render('branch', {
3247
branch,
33-
files: await getFiles(path),
34-
breadCrumbs: await getBreadCrumbs(branch, path),
48+
commits: await getCommits(branch),
49+
files: await getFiles(branch),
3550
});
51+
} catch (error) {
52+
console.error(error);
53+
return res.status(404).render('404');
3654
}
37-
38-
return res.render('branch', {
39-
branch,
40-
commits: await getCommits(branch),
41-
files: await getFiles(branch),
42-
});
4355
});
4456

4557
router.get('/:branch/:commit', async (req, res) => {
4658
const { commit, branch } = req.params;
4759
const { file, path } = req.query;
4860

49-
if (file) {
50-
return res.render('preview', {
61+
try {
62+
if (file) {
63+
return res.render('preview', {
64+
branch,
65+
breadCrumbs: await getBreadCrumbs(branch, file),
66+
fileContent: await getFileContent(file),
67+
});
68+
}
69+
70+
const fileHash = path || commit;
71+
const breadCrumbs = path ? await getBreadCrumbs(commit, fileHash) : null;
72+
73+
return res.render('files', {
5174
branch,
52-
breadCrumbs: await getBreadCrumbs(branch, file),
53-
fileContent: await getFileContent(file),
75+
breadCrumbs,
76+
commit: {
77+
name: await getCommitName(commit),
78+
hash: commit,
79+
},
80+
files: await getFiles(fileHash),
5481
});
82+
} catch (error) {
83+
console.error(error);
84+
return res.status(404).render('404');
5585
}
56-
57-
const fileHash = path || commit;
58-
const breadCrumbs = path ? await getBreadCrumbs(commit, fileHash) : null;
59-
60-
return res.render('files', {
61-
branch,
62-
breadCrumbs,
63-
commit: {
64-
name: await getCommitName(commit),
65-
hash: commit,
66-
},
67-
files: await getFiles(fileHash),
68-
});
6986
});
7087

7188
module.exports = router;

server/views/404.pug

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
extends layout
2+
block content
3+
header
4+
h1.title 404 Not Found
5+
main
6+
h2
7+
a(href="/") Back to main page

0 commit comments

Comments
 (0)