Skip to content

Commit

Permalink
Merge pull request cloudflare#34 from cloudflare/alewis/serve-404
Browse files Browse the repository at this point in the history
Alewis/serve 404
  • Loading branch information
ashleymichal authored and lukeed committed Sep 26, 2019
2 parents 4bc18ad + 6ba329e commit 9446946
Showing 1 changed file with 10 additions and 11 deletions.
21 changes: 10 additions & 11 deletions worker-sites/workers-site/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,19 +43,18 @@ async function handleEvent(event) {
}
return await getAssetFromKV(event, options)
} catch (e) {
if (DEBUG) {
return new Response(e.message || e.toString(), {
status: 404,
})
} else {
let requestedAsset = options.mapRequestToAsset
? options.mapRequestToAsset(event.request).url
: mapRequestToAsset(event.request.url)
// if an error is thrown try to serve the asset at 404.html
if (!DEBUG) {
try {
let notFoundResponse = await getAssetFromKV(event, {
mapRequestToAsset: req => new Request(`${new URL(req.url).origin}/404.html`, req),
})

return new Response(`"${requestedAsset}" not found`, {
status: 404,
})
return new Response(notFoundResponse.body, { ...notFoundResponse, status: 404 })
} catch (e) {}
}

return new Response(e.message || e.toString(), { status: 500 })
}
}

Expand Down

0 comments on commit 9446946

Please sign in to comment.