async function handler(event){
  // Get the request from the client.
  const req = event.request;

  let beresp = await fetch(req, {
    backend: "origin_0",
  });
  // Synthetic html page:
  const synth = `
    <!DOCTYPE html>
    <html lang="en">
      <head>
        <title>There was a problem</title>
        <link href="https://fonts.googleapis.com/css?family=Roboto:300,400,500" rel="stylesheet" type="text/css">
        <style>
          body {
            display: inline-block;
            background: #7000f9 no-repeat;
            height: 100vh;
            margin: 0;
            color: white;
          }
          h1 {
            margin: .8em 3rem;
            font: 4em Roboto;
          }
          p {
            display: inline-block;
            margin: .2em 3rem;
            font: 2em Roboto;
          }
        </style>
      </head>
      <body>
        <h1>451 APOLOGIES!</h1>
        <p>Something went wrong</p>
      </body>
    </html>
  `;

  // if status is 451 Unavailable For Legal Reasons, then return synthetic html page
  if (beresp.status === 451) {
    beresp = new Response(synth, {
      status: 451,
      headers: {
        "Content-Type": "text/html",
      },
    });
    return beresp;
  }
  // if status is 500 Internal server error or 503 Service unavailable, Modify the url to return a custom error page
  else if (beresp.status === 503 || beresp.status === 500 ){
    
    // Store the initial status (500 or 503)
    const status = beresp.status;
    const cacheOverride = new CacheOverride("pass");

    // Get the respsonse
    const clonedBeresp = await fetch("https://httpbin.org/anything/some/testing"+Math.random(), {
      cacheOverride,
      backend: "origin_0"
    });

    // Create a new response with the original status
    const newResponse = new Response(clonedBeresp.body, {
        status,
        headers: clonedBeresp.headers
    });

    return newResponse;
  }
  
}

addEventListener("fetch", (event) => event.respondWith(handler(event)));