Skip to content

Commit 0522dbc

Browse files
conico974Nicolas Dorseuil
andauthored
Handle markdown errors gracefully (#3468)
Co-authored-by: Nicolas Dorseuil <[email protected]>
1 parent acb9f53 commit 0522dbc

File tree

1 file changed

+25
-7
lines changed

1 file changed

+25
-7
lines changed
Lines changed: 25 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,35 @@
11
import type { GitBookSiteContext } from '@/lib/context';
2-
import { throwIfDataError } from '@/lib/data';
2+
import { getDataOrNull } from '@/lib/data';
33
import { getMarkdownForPage } from '@/lib/markdownPage';
44

55
/**
66
* Serve a markdown version of a page.
77
* Returns a 404 if the page is not found.
88
*/
99
export async function servePageMarkdown(context: GitBookSiteContext, pagePath: string) {
10-
const result = await throwIfDataError(getMarkdownForPage(context, pagePath));
10+
try {
11+
const result = await getDataOrNull(getMarkdownForPage(context, pagePath));
12+
if (!result) {
13+
return new Response('Page not found', {
14+
status: 404,
15+
headers: {
16+
'Content-Type': 'text/plain; charset=utf-8',
17+
},
18+
});
19+
}
1120

12-
return new Response(result, {
13-
headers: {
14-
'Content-Type': 'text/markdown; charset=utf-8',
15-
},
16-
});
21+
return new Response(result, {
22+
headers: {
23+
'Content-Type': 'text/markdown; charset=utf-8',
24+
},
25+
});
26+
} catch (error) {
27+
console.error('Error serving markdown page:', error);
28+
return new Response('Internal Server Error', {
29+
status: 500,
30+
headers: {
31+
'Content-Type': 'text/plain; charset=utf-8',
32+
},
33+
});
34+
}
1735
}

0 commit comments

Comments
 (0)