Hi,
My goal is to return the contents of file ‘/error/500.html’ with a HTTP status code of 500 when my code enters a javascript catch block.
The approach is to use getAssetFromKV to get the page contents, along the lines of the example “handlePrefix”.
The problem I’ve got is that I’m still returning the original page contents (with a 500 status), rather than the error page contents.
The function I’m expecting to be used as mapRequestToAsset is:
function serve500Page() {
return request => {
console.log("Running serve500Page...");
// compute the default (e.g. / -> index.html)
let defaultAssetKey = mapRequestToAsset(request)
let url = new URL(defaultAssetKey.url)
// strip the prefix from the path for lookup
url.pathname = "/error/500.html";
// inherit all other props from the default request
return new Request(url.toString(), defaultAssetKey)
}
}
The call to getAssetFromKV is:
let notFoundResponse = await getAssetFromKV(event, {
mapRequestToAsset: serve500Page(),
cacheControl: { bypassCache: true, },
});
let resp = new Response(notFoundResponse.body, { ...notFoundResponse, status: 500 });
console.log("Response body is: " + await resp.text());
Using wrangler to tail the logs, I don’t ever see the output from
“console.log(“Running serve500Page…”);”
The output for response body is:
[“Response body is: <!doctype html>\n\n \n <link rel=“icon” type=“image/x-icon” href=“favicon.ico”>\n\n \n \nPage 3, with some more text\n \n”]
Which is the text of the originally requested page.
Can anyone spot/suggest what I’m doing wrong?
I’m pretty new to Workers Sites (and quite rusty with JavaScript) so I’m likely just being dim…
Cheers, Andy