Illegal invocation error calling gadget API from cloudflare worker

I wonder if anyone else has run into this. We are trying to use our Gadget client from a Cloudflare worker, and we are getting
Illegal invocation: function called with incorrect `this` reference. See https://developers.cloudflare.com/workers/observability/errors/#illegal-invocation-errors for details.`
Illegal invocation: function called with incorrect `this` reference. See https://developers.cloudflare.com/workers/observability/errors/#illegal-invocation-errors for details.`
To isolate the problem, I ran the following in a standalone script:
const api = new ShopkeyClient({
environment: 'development',
authenticationMode: {
apiKey: env.GADGET_API_KEY,
},
fetchImplementation: fetch,
});
const segment = await api.segment.findOne('1');
console.log({segment});
const api = new ShopkeyClient({
environment: 'development',
authenticationMode: {
apiKey: env.GADGET_API_KEY,
},
fetchImplementation: fetch,
});
const segment = await api.segment.findOne('1');
console.log({segment});
…and it works as expected. But then copy that same code into a fetch handler in a Cloudflare worker, and it's having trouble. I wonder if anyone has run into this? Is this a known compatibility issue with the Cloudflare runtime? Any idea if there is a workaround?
6 Replies
airhorns
airhorns5w ago
hey Adam do you have a stack trace for the error? do you know which function is getting called with the wrong this?
Adam Neary
Adam NearyOP5w ago
Here's what I am seeing. looks like a mix of otel instrumentation, urql, and GadgetConnection
Adam Neary
Adam NearyOP4w ago
note: just a regular fetch to the graphql endpoint works fine, so that's been my temporary workaround. @[Gadget] Harry I think for the time being we can close this. I am going to end up killing Axiom (a) because I cannot get it wired up with gadget (wink) and (b) because of this bug. If I just use Sentry for logs/traces and remove the open telemetry code, this problem goes away, and I don't need the graphql workaround. For now I think that's what we'll do. Could be useful as a thought exercise if this comes up again, but for us it's all good. We can just kill otel/axiom to keep trucking.
airhorns
airhorns4w ago
hi @Adam Neary -- we actually just shipped a change late yesterday to try to improve this situation! interally, we used to bind the fetch function, but we stopped doing that now! if you do something to cause your client to issue a new version, and then install that new version into your cloudflare worker package, you may find its fixed! we used to do something like this:
const fetchImplementation = globalThis.fetch.bind(globalThis);
const fetchImplementation = globalThis.fetch.bind(globalThis);
which worked everywhere for years until you reported this issue, but now we do this:
const fetchImplementation = (...args) => globalThis.fetch(...args)
const fetchImplementation = (...args) => globalThis.fetch(...args)
Adam Neary
Adam NearyOP4w ago
Awesome. Thanks for following up on this. Sounds like a cleaner implementation. Good stuff!
Gizmo
Gizmo4d ago
Do you like this answer? ​

Did you find this page helpful?