decolua/9router · error · Error

`CodeBuddy state request failed: ${await response.text()}`

Error message

`CodeBuddy state request failed: ${await response.text()}`

What it means

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.

Source

Thrown at src/lib/oauth/providers/codebuddy-cn.js:25

const codebuddyCn = {
  config: CODEBUDDY_CONFIG,
  flowType: "device_code",
  requestDeviceCode: async (config) => {
    const response = await fetch(`${config.stateUrl}?platform=${config.platform}`, {
      method: "POST",
      headers: {
        "Content-Type": "application/json",
        Accept: "application/json",
        "User-Agent": config.userAgent,
        "X-Requested-With": "XMLHttpRequest",
        "X-Domain": "copilot.tencent.com",
        "X-No-Authorization": "true",
        "X-No-User-Id": "true",
        "X-Product": "SaaS",
      },
      body: "{}",
    });
    if (!response.ok) throw new Error(`CodeBuddy state request failed: ${await response.text()}`);
    const data = await response.json();
    if (data.code !== 0 || !data.data?.state || !data.data?.authUrl) {
      throw new Error(`CodeBuddy state error: ${data.msg || "missing state/authUrl"}`);
    }
    return {
      device_code: data.data.state,
      verification_uri: data.data.authUrl,
      user_code: "",
      interval: config.pollInterval / 1000,
      _isCodeBuddy: true,
    };
  },
  pollToken: async (config, deviceCode) => {
    // CodeBuddy polls the token endpoint via GET with the state as a query
    // param (not POST/body) — matches the official CLI's /v2/plugin/auth/token?state=...
    const response = await fetch(`${config.tokenUrl}?state=${encodeURIComponent(deviceCode)}`, {
      method: "GET",
      headers: {

View on GitHub (pinned to 90b52e06ff)

Solutions

  1. Inspect the thrown body text to see whether it is an auth rejection, 404 (wrong endpoint), or 5xx outage.
  2. Verify network/proxy access to the CodeBuddy CN base URL (curl the state endpoint directly).
  3. Confirm the provider config (stateUrl/base URL) matches the current CodeBuddy API version.
  4. Retry later if the body indicates rate limiting or a temporary 5xx.

Example fix

// before
if (!response.ok) throw new Error(`CodeBuddy state request failed: ${await response.text()}`);
// after
if (!response.ok) {
  const t = await response.text();
  throw new Error(`CodeBuddy state request failed (HTTP ${response.status}): ${t.slice(0, 500)}`);
}
Defensive patterns

Strategy: retry

Validate before calling

// Probe reachability before starting the device flow
const ok = await fetch(config.stateUrl, { method: "HEAD" })
  .then(r => r.ok || r.status < 500).catch(() => false);
if (!ok) throw new Error("CodeBuddy CN state endpoint unreachable — check network/proxy");

Type guard

function hasValidDeviceState(data) {
  return !!data && typeof data === "object" &&
    data.code === 0 && typeof data.data?.state === "string" &&
    typeof data.data?.authUrl === "string";
}

Try / catch

try {
  const state = await startCodeBuddyFlow();
} catch (err) {
  if (String(err.message).includes("CodeBuddy state request failed")) {
    // transport-level: transient — retry with backoff
    await retryWithBackoff(() => startCodeBuddyFlow(), { retries: 3 });
  } else throw err;
}

Prevention

When it happens

Trigger: 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.

Common situations: 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.

Related errors


AI-assisted analysis of decolua/9router@90b52e06ff (2026-08-30). Data as JSON: /api/errors/52fe7236257f1269. Report an issue: GitHub.