workers-sdk icon indicating copy to clipboard operation
workers-sdk copied to clipboard

`wrangler dev` throws an internal error when reporting a worker error for a HEAD request

Open jyn514 opened this issue 8 months ago • 1 comments

Which Cloudflare product(s) does this pertain to?

Wrangler

What versions & operating system are you using?

wrangler 4.10.0

Please provide a link to a minimal reproduction

https://github.com/jyn514/redirect-worker/tree/super-broken

Describe the Bug

my worker has an internal error, which i want to see the message for. i ran curl -I to make it show up in the logs. instead of seeing my worker's error, i see a syntax error in wrangler internals:

[wrangler:err] SyntaxError: Unexpected end of JSON input
    at JSON.parse (<anonymous>)
    at parseJSONFromBytes (/home/jyn/Documents/node-v20.12.2-linux-x64/lib/node_modules/wrangler/node_modules/undici/lib/fetch/body.js:590:15)
    at successSteps (/home/jyn/Documents/node-v20.12.2-linux-x64/lib/node_modules/wrangler/node_modules/undici/lib/fetch/body.js:530:23)
    at fullyReadBody (/home/jyn/Documents/node-v20.12.2-linux-x64/lib/node_modules/wrangler/node_modules/undici/lib/fetch/util.js:959:5)
    at process.processTicksAndRejections (node:internal/process/task_queues:95:5)
    at async specConsumeBody (/home/jyn/Documents/node-v20.12.2-linux-x64/lib/node_modules/wrangler/node_modules/undici/lib/fetch/body.js:545:3)
    at async handlePrettyErrorRequest (/home/jyn/Documents/node-v20.12.2-linux-x64/lib/node_modules/wrangler/node_modules/miniflare/dist/src/index.js:11164:40)
    at async #handleLoopback (/home/jyn/Documents/node-v20.12.2-linux-x64/lib/node_modules/wrangler/node_modules/miniflare/dist/src/index.js:15458:20)
[wrangler:inf] HEAD / 500 Internal Server Error (3ms)

if i do a GET instead of a HEAD, i see the expected worker error:

[wrangler:err] ReferenceError: Url is not defined
    at Object.fetch (file:///home/jyn/src/redirect-worker/src/worker.js:32:18)
    at fetchDispatcher (file:///home/jyn/src/redirect-worker/.wrangler/tmp/bundle-HPW4lZ/middleware-loader.entry.ts:54:17)
[wrangler:inf] GET / 500 Internal Server Error (9ms)

steps to replicate:

  1. clone the repo in the issue description
  2. wrangler dev
  3. curl -I localhost:8787

Please provide any relevant error logs

n/a

jyn514 avatar Apr 12 '25 14:04 jyn514

Still a problem with wrangler 4.53.0.

The bug makes debugging slightly more difficult when working with a worker that proxies a large response. Workaround is to do a GET request while throwing out the potentially large response, e.g. curl localhost:8787 -o /dev/null

Minimal reproducer:

wrangler.jsonc

{
	"name": "head-issue",
	"main": "src/index.js",
	"compatibility_date": "2025-12-05"
}

src/index.js

export default {
    fetch(request, env, ctx) {
        throw new Error();
    },
};

Run wrangler dev followed by curl localhost:8787 and curl localhost:8787 -I

Expected output (for GET) vs actual output (for HEAD):

[wrangler:error] Error
    at Object.fetch (file:///private/tmp/head-issue/src/index.js:3:15)
[wrangler:info] GET / 500 Internal Server Error (16ms)
[wrangler:error] SyntaxError: Unexpected end of JSON input
    at JSON.parse (<anonymous>)
    at parseJSONFromBytes (/opt/homebrew/Cellar/cloudflare-wrangler/4.53.0/libexec/lib/node_modules/wrangler/node_modules/undici/lib/web/fetch/body.js:510:15)
    at successSteps (/opt/homebrew/Cellar/cloudflare-wrangler/4.53.0/libexec/lib/node_modules/wrangler/node_modules/undici/lib/web/fetch/body.js:471:23)
    at readAllBytes (/opt/homebrew/Cellar/cloudflare-wrangler/4.53.0/libexec/lib/node_modules/wrangler/node_modules/undici/lib/web/fetch/util.js:1250:9)
    at process.processTicksAndRejections (node:internal/process/task_queues:103:5)
[wrangler:info] HEAD / 500 Internal Server Error (2ms)

Lekensteyn avatar Dec 08 '25 14:12 Lekensteyn