@@ -33,6 +33,11 @@ async function getArticleBody(req: ExtendedRequestWithPageInfo) {
33
33
// and is in the ExtendedRequestWithPageInfo
34
34
const { page, pathname } = req . pageinfo
35
35
36
+ // for anything that's not an article (like index pages), don't try to render and
37
+ // tell the user what's going on
38
+ if ( page . documentType !== 'article' ) {
39
+ throw new Error ( `Page ${ pathname } isn't yet available in markdown.` )
40
+ }
36
41
// these parts allow us to render the page
37
42
const mockedContext : Context = { }
38
43
const renderingReq = {
@@ -69,7 +74,12 @@ router.get(
69
74
catchMiddlewareError ( async function ( req : ExtendedRequestWithPageInfo , res : Response ) {
70
75
// First, fetch metadata
71
76
const metaData = await getArticleMetadata ( req )
72
- const bodyContent = await getArticleBody ( req )
77
+ let bodyContent
78
+ try {
79
+ bodyContent = await getArticleBody ( req )
80
+ } catch ( error ) {
81
+ return res . status ( 403 ) . json ( { error : ( error as Error ) . message } )
82
+ }
73
83
74
84
defaultCacheControl ( res )
75
85
return res . json ( {
@@ -84,9 +94,14 @@ router.get(
84
94
pathValidationMiddleware as RequestHandler ,
85
95
pageValidationMiddleware as RequestHandler ,
86
96
catchMiddlewareError ( async function ( req : ExtendedRequestWithPageInfo , res : Response ) {
87
- const rendered = await getArticleBody ( req )
97
+ let bodyContent
98
+ try {
99
+ bodyContent = await getArticleBody ( req )
100
+ } catch ( error ) {
101
+ return res . status ( 403 ) . json ( { error : ( error as Error ) . message } )
102
+ }
88
103
defaultCacheControl ( res )
89
- return res . type ( 'text/markdown' ) . send ( rendered )
104
+ return res . type ( 'text/markdown' ) . send ( bodyContent )
90
105
} ) ,
91
106
)
92
107
0 commit comments