{"record":{"id":"be0b3884d3d65f38","repo":"decolua/9router","slug":"codebuddy-state-error-data-msg-missing-sta","errorCode":null,"errorMessage":"`CodeBuddy state error: ${data.msg || \"missing state/authUrl\"}`","messagePattern":"`CodeBuddy state error: (.+?)`","errorType":"exception","errorClass":"Error","httpStatus":null,"severity":"error","filePath":"src/lib/oauth/providers/codebuddy-cn.js","lineNumber":28,"sourceCode":"  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: {\n        Accept: \"application/json\",\n        \"User-Agent\": config.userAgent,\n        \"X-Requested-With\": \"XMLHttpRequest\",","sourceCodeStart":10,"sourceCodeEnd":46,"githubUrl":"https://github.com/decolua/9router/blob/90b52e06ffd666b7929554211474d01588f6b1f8/src/lib/oauth/providers/codebuddy-cn.js#L10-L46","documentation":"CodeBuddy (CN) returned HTTP 200 but an application-level error: the JSON envelope's code !== 0, or data.data.state / data.data.authUrl is missing. The provider maps the envelope's msg field into this Error, falling back to 'missing state/authUrl' when msg is absent. Unlike [201], the HTTP layer succeeded — the API refused the request in its payload.","triggerScenarios":"Successful POST to the state endpoint whose body parses to { code: non-zero } or lacks data.state/data.authUrl — e.g. service degraded but returning 200, API version change renaming fields, or region/account restrictions encoded in code/msg.","commonSituations":"CodeBuddy updated their response schema (state/authUrl renamed or nested differently); account/region not permitted to use device login; server returns {code:500,...} with a Chinese-language msg; proxy altering the response.","solutions":["Read data.msg in the error message — it states the API's own reason.","Log the full response JSON and compare against the expected envelope {code:0, data:{state, authUrl}}; update field paths if the schema changed.","Retry the state request — transient application-level errors often clear.","Check for a CodeBuddy client/API update and bump the provider config accordingly."],"exampleFix":"// before\nif (data.code !== 0 || !data.data?.state || !data.data?.authUrl) {\n  throw new Error(`CodeBuddy state error: ${data.msg || \"missing state/authUrl\"}`);\n}\n// after\nif (data.code !== 0 || !data.data?.state || !data.data?.authUrl) {\n  throw new Error(`CodeBuddy state error: ${data.msg || \"missing state/authUrl\"} (code=${data.code}, keys=${Object.keys(data.data || {})})`);\n}","handlingStrategy":"validation","validationCode":"// Validate the envelope before relying on it\nconst data = await response.json();\nif (typeof data.code !== \"number\" || !data.data?.state || !data.data?.authUrl) {\n  console.error(\"CodeBuddy CN envelope:\", JSON.stringify(data).slice(0, 1000));\n}","typeGuard":"function isCodeBuddyStateEnvelope(v) {\n  return !!v && typeof v === \"object\" && v.code === 0 &&\n    typeof v.data === \"object\" && v.data !== null &&\n    typeof v.data.state === \"string\" && v.data.state.length > 0 &&\n    typeof v.data.authUrl === \"string\" && v.data.authUrl.startsWith(\"http\");\n}","tryCatchPattern":"try {\n  const s = await startCodeBuddyFlow();\n} catch (err) {\n  if (String(err.message).includes(\"CodeBuddy state error\")) {\n    // application-level: msg carries the API reason; do not blind-retry\n    console.error(\"CodeBuddy CN API refused state request:\", err.message);\n  } else throw err;\n}","preventionTips":["Log the full response envelope when validation fails to catch schema drift early.","Validate data.state/data.authUrl types before use, not just presence.","Track upstream CodeBuddy client releases for response-shape changes.","Distinguish application errors (code!=0) from transport errors — only the latter are retry-safe."],"tags":["oauth","device-flow","api-schema","response-validation"],"backgroundTag":"api-schema-validation-failed","analyzedSha":"90b52e06ffd666b7929554211474d01588f6b1f8","analyzedAt":"2026-08-30T21:05:45.952Z","schemaVersion":2},"datasetVersion":"2026-08-30T23:17:21.991Z"}