I really enjoy using Cloudflare Workers. However, I can’t figure out how to handle certain origin fetch errors. In below example a nonexistent remote host is used. Since connection attempts will fail, I would expect to get a 503 response but whatever I try, the response is always a Cloudflare error page (1016 and 502 in below case) and not my desired custom error handling response.
async function handleRequest(request) {
let subrequest = new Request("http://djskdjhksgdksjhdkjh.com", request)
try {
return await fetch(subrequest)
} catch (exception) {
return new Response("Service temporarily unavailable", {status: 503})
}
}
addEventListener('fetch', event => {
event.respondWith(handleRequest(event.request))
})
Actual use case: I am trying to implement a Worker that returns 503 if the origin can’t be reached / is down. How do I do that?