Worker threw exception / Body has already been used

Hello! I created this Worker to detect when a page on my website has the string data-error="404" in the body and set the appropriate HTTP response code. I also created a Worker Route for mysite.tld/* and this works perfectly for error pages that have the data-error string, but for "normal" pages the error "Error 1101 Worker threw exception" is generated. In the Worker's Quick Edit console I also see the error "Uncaught (in response) TypeError: Body has already been used. It can only be used once. Use tee() first if you need to read it twice.", but I really couldn't wrap my head around resolving this. Any help?

export default {
   async fetch(request) {
     const response = await fetch(request)
     const body = await response.text()
     if (body.includes("html data-error=\"404\"")) {
       return new Response(body, {
         status: 404,
         statusText: "Not Found",
         headers: response.headers
       })
     } else {
       return response
     }
   }
};
Was this page helpful?