cloudflare / cloudflare/workerd

bug: devtools logs some errors even when they're caught

Open
#424 0 comments 0 reactions 0 assignees View on GitHub
bug
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 -
![image](https://user-images.githubusercontent.com/18808/168986587-65a384e8-7c5b-458d-a03f-04567e4c4612.png)

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

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.