-
Notifications
You must be signed in to change notification settings - Fork 760
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
🐛 BUG: calling fetch inside fetch handler throws internal error #7787
Comments
Maybe try to update wrangler |
Thanks @vicb! Updated to |
export default {
async fetch(request, env, ctx): Promise<Response> {
const someHost = 'https://jsonplaceholder.typicode.com';
const url1 = someHost + '/todos/1';
const url2 = someHost + '/todos/2';
const responses = await Promise.all([fetch(url1), fetch(url2)]);
const results = await Promise.all(responses.map((r) => r.json()));
const options = {
headers: { 'content-type': 'application/json;charset=UTF-8' },
};
return new Response(JSON.stringify(results), options);
},
}; works for me. Can you try to clear your cache (or incognito), try/cache and log the exception details? |
This is the exception trace @vicb: Error: internal error
at [object Object]
at async Object.fetch (file:///Users/src/cf-worker-bug-repro/src/index.ts:8:21)
at async jsonError (file:///Users/src/cf-worker-bug-repro/node_modules/.pnpm/wrangler@3.103.0_@cloudflare+workers-types@4.20250109.0/node_modules/wrangler/templates/middleware/middleware-miniflare3-json-error.ts:22:10)
at async drainBody (file:///Users/src/cf-worker-bug-repro/node_modules/.pnpm/wrangler@3.103.0_@cloudflare+workers-types@4.20250109.0/node_modules/wrangler/templates/middleware/middleware-ensure-req-body-drained.ts:5:10)% I'm using postman so no cookies or cache |
Also Is it possible that this won't work due to firewalls? |
I think you should use GET instead, not sure if this can be the root cause |
More trace,
|
You should try to log response.ok and response.statusText |
Error is happening in Here's a screen recording: cf-error.mov |
Is there a way to set up another http client other than fetch? |
@hasangenc0 I've cloned your repro and everything seems to work for me. To clarify, your reproduction talks about a GET request, but the screenshot of Postman you've included has a body and sends a POST request. Can you reproduce this with just the example reproduction you provided? |
@hasangenc0 I played with https://github.com/hasangenc0/cf-worker-bug-repro and found that I could reproduce the behavior you reported by setting This suggests that the problem may be caused by your local network not being able to resolve the endpoint in the code, or some other network failure which would cause fetch() to throw. You might be able to repro this with The issue in wrangler seems to be that the stack trace for the fetch error is not captured properly, so the error message is misleading. This becomes more obvious if you catch and return the error instead of letting the worker fail. export default {
async fetch(request) {
// someHost is set up to return JSON responses
const someHost = 'https://jsonplaceholder.typicode.coms';
const url1 = someHost + '/todos/1';
const url2 = someHost + '/todos/2';
let results;
try {
const resp1 = await fetch(url1);
const resp2 = await fetch(url2);
const json1 = await resp1.json();
const json2 = await resp2.json();
results = JSON.stringify([json1, json2], null, 2);
} catch (e: any) {
results = `${e.message}\n${e.stack}`;
}
return new Response(results);
},
} satisfies ExportedHandler; returns internal error
Error: internal error
at async Object.fetch (file:///Users/jldec/cloudflare/issue-7787/.wrangler/tmp/dev-PofTSg/index.js:12:21)
at async jsonError (file:///Users/jldec/cloudflare/issue-7787/.wrangler/tmp/dev-PofTSg/index.js:55:12)
at async drainBody (file:///Users/jldec/cloudflare/issue-7787/.wrangler/tmp/dev-PofTSg/index.js:28:12) |
@penalosa I'm actually sending GET request and check the screenshot in the description and here, don't know why the error page shows POST. To clarify everything I recorded the e2e experience :) I send GET with no request body, using https://github.com/hasangenc0/cf-worker-bug-repro. (updated to @jldec's example) |
@jldec that's exactly what I can't figure out, curl just works, so I don't think there's an issue with the local network? cf-cf.mov |
Btw without the extra (s) is the same for me, see below: Screen.Recording.2025-01-17.at.22.56.02.movIs there any way that I can debug the
|
Oh wow, replacing https to http worked! Does anyone know why? export default {
async fetch(request) {
// someHost is set up to return JSON responses
const someHost = 'http://jsonplaceholder.typicode.com';
const url1 = someHost + '/todos/1';
const url2 = someHost + '/todos/2';
let results;
try {
const resp1 = await fetch(url1);
const resp2 = await fetch(url2);
const json1 = await resp1.json();
const json2 = await resp2.json();
results = JSON.stringify([json1, json2], null, 2);
} catch (e: any) {
results = `${e.message}\n${e.stack}`;
}
return new Response(results);
},
} satisfies ExportedHandler; |
Which Cloudflare product(s) does this pertain to?
Workers Runtime
What versions are you using?
3.103.0 [wrangler] 23.1.0 [node]
What operating system and version are you using?
Apple M1 Max MacOS Sonoma 14.7.2 (23H311)
Please provide a link to a minimal reproduction
https://github.com/hasangenc0/cf-worker-bug-repro
Describe the Bug
Just copy paste the example code in this URL.
Also repro here.
Run worker, then make a request.
You will see
Error: internal error
I'm wondering is that a outdated documentation or it's a real bug. But I'm sure that calling
fetch
insidefetch
definitely causes this issue.Please provide any relevant error logs
No response
The text was updated successfully, but these errors were encountered: