cloudflare / cloudflare/workerd
bug: devtools logs some errors even when they're caught
- Dominant language
- C++
- Stars
- 8.7k
- Forks
- 739
- Avg merge
- 2d 20h
- Merged PRs (30d)
- 174
Description
(Reported by Mozzy#9999 on discord)
Given this script
```js
addEventListener("fetch", (event) => {
event.respondWith(handleRequest(event.request));
});
async function handleRequest(request) {
if (request.method === "POST") {
// Issue(?) with "wrangler dev":
// Expected: Send a POST request with an empty body or a body that has invalid JSON -> catch and return 400 with no errors reported
// Reality: Send a POST request with an empty body or a body that has invalid JSON -> reports "X [ERROR] Uncaught SyntaxError: Unexpected end of JSON input" in the console, AND returns the 400
// Post request is sent via Postman
let invalidBody = false;
const { item1, item2 } = await request.json().catch((e) => {
console.log("xxx", e);
invalidBody = true;
return {};
});
if (invalidBody)
return new Response(
"invalidBody is true, meaning request.json() failed.",
{ headers: { "Content-Type": "text/plain" }, status: 400 }
);
return new Response(JSON.stringify({ item1, item2 }), {
headers: { "Content-Type": "application/json" },
status: 200,
});
}
if (request.method === "GET")
return new Response("I see your GET.", {
headers: { "Content-Type": "text/plain" },
status: 200,
});
}
```
Run the above with `wrangler dev`.
Make another script
```js
// post.js
async function run() {
console.log(
await (await fetch("http://localhost:8787", { method: "POST" })).text()
);
}
run();
```
Call it with `node post.js`.
You'll see this in the terminal/devtools -

Depite having the `.catch()`. The worker itself doesn't error, and returns the response as requested.
So it looks like our runtime is being a little overenthusiastic here and sending that logging message across the wire. It can be confusing to a dev, so we should fix this.
Contributor guide
Assessment
This issue has not been assessed yet.