{"record":{"id":"fd24c3849328f921","repo":"chroma-core/chroma","slug":"error-calling-cloudflare-workers-ai-api-error","errorCode":null,"errorMessage":"Error calling Cloudflare Workers AI API: ${error}","messagePattern":"Error calling Cloudflare Workers AI API: (.+?)","errorType":"exception","errorClass":"Error","httpStatus":null,"severity":"error","filePath":"clients/js/packages/chromadb-core/src/embeddings/CloudflareWorkersAIEmbeddingFunction.ts","lineNumber":89,"sourceCode":"        method: \"POST\",\n        headers: this.headers,\n        body: JSON.stringify(payload),\n      });\n\n      const resp = await response.json();\n\n      if (!resp.result || !resp.result.data) {\n        throw new Error(resp.detail || \"Unknown error\");\n      }\n\n      return resp.result.data;\n    } catch (error) {\n      if (error instanceof Error) {\n        throw new Error(\n          `Error calling Cloudflare Workers AI API: ${error.message}`,\n        );\n      } else {\n        throw new Error(`Error calling Cloudflare Workers AI API: ${error}`);\n      }\n    }\n  }\n\n  buildFromConfig(config: StoredConfig): CloudflareWorkersAIEmbeddingFunction {\n    return new CloudflareWorkersAIEmbeddingFunction({\n      model_name: config.model_name,\n      account_id: config.account_id,\n      api_key_env_var: config.api_key_env_var,\n      gateway_id: config.gateway_id ?? undefined,\n    });\n  }\n\n  getConfig(): StoredConfig {\n    return {\n      model_name: this.model_name,\n      account_id: this.account_id,\n      api_key_env_var: this.api_key_env_var,","sourceCodeStart":71,"sourceCodeEnd":107,"githubUrl":"https://github.com/chroma-core/chroma/blob/aecdd12c8a891610db8653630b066b32ceb678b5/clients/js/packages/chromadb-core/src/embeddings/CloudflareWorkersAIEmbeddingFunction.ts#L71-L107","documentation":"The else-branch of the same wrapper in CloudflareWorkersAIEmbeddingFunction.generate: when the caught value is NOT an instance of Error (e.g. a thrown string, number, or object - possible from exotic fetch implementations, monkey-patched fetch, or environment quirks like some DOMException variants), it is stringified into the message 'Error calling Cloudflare Workers AI API: ${error}'. Rare compared to the Error branch; same debugging approach applies.","triggerScenarios":"Custom/edge fetch implementations (workers, undici forks) rejecting with non-Error values; test doubles that throw plain objects; older runtimes where a rejection reason loses its Error prototype.","commonSituations":"Running the client inside non-Node runtimes (Cloudflare Workers themselves, Deno compatibility layers); unit tests with vi.fn() mocks rejecting with strings.","solutions":["Inspect the stringified value in the message suffix to identify what was actually thrown.","If you control the fetch polyfill/mock, make it reject with real Error objects.","Treat it like the Error-branch wrapper: network/API debugging plus retry logic where appropriate."],"exampleFix":"// before (test mock)\nvi.stubGlobal('fetch', vi.fn(async () => { throw 'boom'; }));\n\n// after\nvi.stubGlobal('fetch', vi.fn(async () => { throw new Error('boom'); }));","handlingStrategy":"try-catch","validationCode":null,"typeGuard":null,"tryCatchPattern":"try {\n  await ef.generate(texts);\n} catch (e) {\n  const msg = e instanceof Error ? e.message : String(e);\n  if (msg.startsWith('Error calling Cloudflare Workers AI API:')) {\n    // non-Error rejection: inspect the stringified suffix to find the real cause\n    // if running in tests, fix fetch mocks to reject with real Error objects\n  }\n  throw e;\n}","preventionTips":["Make custom fetch implementations and test mocks always reject with Error instances.","In edge runtimes, smoke-test the embedding path in the actual runtime, not just Node.","Log the full wrapped message; the suffix identifies the thrown non-Error value."],"tags":["embeddings","cloudflare","error-wrapping","non-error-throw","runtime"],"backgroundTag":"api-request-failed","analyzedSha":"aecdd12c8a891610db8653630b066b32ceb678b5","analyzedAt":"2026-08-16T21:53:27.228Z","schemaVersion":2},"datasetVersion":"2026-08-16T23:17:17.608Z"}