Skip to content

Commit 70f09d9

Browse files
authored
Fix improperly formatted response for pages unavailable in markdown (#54689)
1 parent 0dbd5a6 commit 70f09d9

File tree

1 file changed

+18
-3
lines changed

1 file changed

+18
-3
lines changed

src/article-api/middleware/article.ts

+18-3
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,11 @@ async function getArticleBody(req: ExtendedRequestWithPageInfo) {
3333
// and is in the ExtendedRequestWithPageInfo
3434
const { page, pathname } = req.pageinfo
3535

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+
}
3641
// these parts allow us to render the page
3742
const mockedContext: Context = {}
3843
const renderingReq = {
@@ -69,7 +74,12 @@ router.get(
6974
catchMiddlewareError(async function (req: ExtendedRequestWithPageInfo, res: Response) {
7075
// First, fetch metadata
7176
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+
}
7383

7484
defaultCacheControl(res)
7585
return res.json({
@@ -84,9 +94,14 @@ router.get(
8494
pathValidationMiddleware as RequestHandler,
8595
pageValidationMiddleware as RequestHandler,
8696
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+
}
88103
defaultCacheControl(res)
89-
return res.type('text/markdown').send(rendered)
104+
return res.type('text/markdown').send(bodyContent)
90105
}),
91106
)
92107

0 commit comments

Comments
 (0)