{"record":{"id":"52fe7236257f1269","repo":"decolua/9router","slug":"codebuddy-state-request-failed-await-response","errorCode":null,"errorMessage":"`CodeBuddy state request failed: ${await response.text()}`","messagePattern":"`CodeBuddy state request failed: (.+?)`","errorType":"exception","errorClass":"Error","httpStatus":null,"severity":"error","filePath":"src/lib/oauth/providers/codebuddy-cn.js","lineNumber":25,"sourceCode":"const codebuddyCn = {\n  config: CODEBUDDY_CONFIG,\n  flowType: \"device_code\",\n  requestDeviceCode: async (config) => {\n    const response = await fetch(`${config.stateUrl}?platform=${config.platform}`, {\n      method: \"POST\",\n      headers: {\n        \"Content-Type\": \"application/json\",\n        Accept: \"application/json\",\n        \"User-Agent\": config.userAgent,\n        \"X-Requested-With\": \"XMLHttpRequest\",\n        \"X-Domain\": \"copilot.tencent.com\",\n        \"X-No-Authorization\": \"true\",\n        \"X-No-User-Id\": \"true\",\n        \"X-Product\": \"SaaS\",\n      },\n      body: \"{}\",\n    });\n    if (!response.ok) throw new Error(`CodeBuddy state request failed: ${await response.text()}`);\n    const data = await response.json();\n    if (data.code !== 0 || !data.data?.state || !data.data?.authUrl) {\n      throw new Error(`CodeBuddy state error: ${data.msg || \"missing state/authUrl\"}`);\n    }\n    return {\n      device_code: data.data.state,\n      verification_uri: data.data.authUrl,\n      user_code: \"\",\n      interval: config.pollInterval / 1000,\n      _isCodeBuddy: true,\n    };\n  },\n  pollToken: async (config, deviceCode) => {\n    // CodeBuddy polls the token endpoint via GET with the state as a query\n    // param (not POST/body) — matches the official CLI's /v2/plugin/auth/token?state=...\n    const response = await fetch(`${config.tokenUrl}?state=${encodeURIComponent(deviceCode)}`, {\n      method: \"GET\",\n      headers: {","sourceCodeStart":7,"sourceCodeEnd":43,"githubUrl":"https://github.com/decolua/9router/blob/90b52e06ffd666b7929554211474d01588f6b1f8/src/lib/oauth/providers/codebuddy-cn.js#L7-L43","documentation":"CodeBuddy (CN) device-flow bootstrap: the POST that requests a device 'state' (device_code + authUrl) returned a non-2xx HTTP status. The raw response body is awaited inline and thrown inside this Error, so the message carries the server's response text. This fires before any polling starts.","triggerScenarios":"Calling the startDeviceFlow/requestState function for codebuddy-cn when the state endpoint responds !response.ok — network failure, gateway 404/502, or server-side rejection of the anonymous headers (X-No-Authorization, X-No-User-Id, X-Product: SaaS) with body '{}' posted.","commonSituations":"Corporate proxy or firewall blocking the CodeBuddy CN endpoint; endpoint URL changed in a provider-side update; regional endpoint unreachable from outside China; TLS interception producing 4xx/5xx with an HTML body.","solutions":["Inspect the thrown body text to see whether it is an auth rejection, 404 (wrong endpoint), or 5xx outage.","Verify network/proxy access to the CodeBuddy CN base URL (curl the state endpoint directly).","Confirm the provider config (stateUrl/base URL) matches the current CodeBuddy API version.","Retry later if the body indicates rate limiting or a temporary 5xx."],"exampleFix":"// before\nif (!response.ok) throw new Error(`CodeBuddy state request failed: ${await response.text()}`);\n// after\nif (!response.ok) {\n  const t = await response.text();\n  throw new Error(`CodeBuddy state request failed (HTTP ${response.status}): ${t.slice(0, 500)}`);\n}","handlingStrategy":"retry","validationCode":"// Probe reachability before starting the device flow\nconst ok = await fetch(config.stateUrl, { method: \"HEAD\" })\n  .then(r => r.ok || r.status < 500).catch(() => false);\nif (!ok) throw new Error(\"CodeBuddy CN state endpoint unreachable — check network/proxy\");","typeGuard":"function hasValidDeviceState(data) {\n  return !!data && typeof data === \"object\" &&\n    data.code === 0 && typeof data.data?.state === \"string\" &&\n    typeof data.data?.authUrl === \"string\";\n}","tryCatchPattern":"try {\n  const state = await startCodeBuddyFlow();\n} catch (err) {\n  if (String(err.message).includes(\"CodeBuddy state request failed\")) {\n    // transport-level: transient — retry with backoff\n    await retryWithBackoff(() => startCodeBuddyFlow(), { retries: 3 });\n  } else throw err;\n}","preventionTips":["Configure proxy/firewall exceptions for the CodeBuddy CN domain before login.","Retry transport failures with exponential backoff — they are usually transient.","Pin and periodically review the provider's state URL against upstream changes.","Surface HTTP status in logs, not just the body text."],"tags":["oauth","device-flow","http-error","network"],"backgroundTag":"oauth-device-flow-request-failed","analyzedSha":"90b52e06ffd666b7929554211474d01588f6b1f8","analyzedAt":"2026-08-30T21:05:45.952Z","schemaVersion":2},"datasetVersion":"2026-08-30T23:17:21.991Z"}