Francisco
Francisco
CDCloudflare Developers
Created by Francisco on 1/8/2024 in #workers-help
Worker threw exception / Body has already been used
So I've reached the goal with this abomination of code, I have no idea how to improve it so I'll leave it at that - Luckily there are no errors on the console.
export default {
async fetch(request) {
let response = await fetch(request);
let contentType = response.headers.get("content-type");
if (contentType && contentType.includes("text/html")) {
let body = await response.text();
let dataErrorMatch = body.match(/html data-error="(\d+)"/);
if (dataErrorMatch && dataErrorMatch[1]) {
let httpStatusCode = parseInt(dataErrorMatch[1], 10);
if (!isNaN(httpStatusCode) && httpStatusCode >= 100 && httpStatusCode < 600) {
return new Response(body, {
status: httpStatusCode,
statusText: response.statusText,
headers: response.headers,
});
}
}
return new Response(body, response);
} else {
return response;
}
},
};
export default {
async fetch(request) {
let response = await fetch(request);
let contentType = response.headers.get("content-type");
if (contentType && contentType.includes("text/html")) {
let body = await response.text();
let dataErrorMatch = body.match(/html data-error="(\d+)"/);
if (dataErrorMatch && dataErrorMatch[1]) {
let httpStatusCode = parseInt(dataErrorMatch[1], 10);
if (!isNaN(httpStatusCode) && httpStatusCode >= 100 && httpStatusCode < 600) {
return new Response(body, {
status: httpStatusCode,
statusText: response.statusText,
headers: response.headers,
});
}
}
return new Response(body, response);
} else {
return response;
}
},
};
Thank you Chaika for the help MeowHeartCloudflare Marking the topic as Solved.
6 replies
CDCloudflare Developers
Created by Francisco on 1/8/2024 in #workers-help
Worker threw exception / Body has already been used
My code so bad I put Cloudflare down 😂 https://www.cloudflarestatus.com/
6 replies
CDCloudflare Developers
Created by Francisco on 1/8/2024 in #workers-help
Worker threw exception / Body has already been used
Is this better or worse than the original code (fortunately this modification solved the problem!)?
export default {
async fetch(request) {
const response = await fetch(request);
const contentType = response.headers.get("content-type");
if (contentType && contentType.includes("text/html")) {
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 new Response(body, response);
}
} else {
return response;
}
},
};
export default {
async fetch(request) {
const response = await fetch(request);
const contentType = response.headers.get("content-type");
if (contentType && contentType.includes("text/html")) {
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 new Response(body, response);
}
} else {
return response;
}
},
};
Disclaimer: yes this garbage was generated by ChatGPT, I'm still not that good with JavaScript
6 replies