chenhg5/cc-connect · error

yuanbao: parse response: %w

Error message

yuanbao: parse response: %w

What it means

The 200 response body from the yuanbao sign-token API could not be decoded into the expected {code, data:{token,bot_id,duration,product,source}} envelope. json.Unmarshal failed, the error is wrapped and the loop retries; after maxRetries the last error is returned. This signals the endpoint returned JSON in an unexpected shape or non-JSON content.

Source

Thrown at platform/yuanbao/sign.go:175

		respBody, _ := io.ReadAll(resp.Body)
		_ = resp.Body.Close()
		if resp.StatusCode != http.StatusOK {
			lastErr = fmt.Errorf("yuanbao: sign token API returned %d: %s", resp.StatusCode, string(respBody))
			continue
		}

		var result struct {
			Code int `json:"code"`
			Data *struct {
				Token    string `json:"token"`
				BotID    string `json:"bot_id"`
				Duration int    `json:"duration"`
				Product  string `json:"product"`
				Source   string `json:"source"`
			} `json:"data"`
		}
		if err := json.Unmarshal(respBody, &result); err != nil {
			lastErr = fmt.Errorf("yuanbao: parse response: %w", err)
			continue
		}
		if result.Code == 10099 && attempt < maxRetries {
			continue
		}
		if result.Code != 0 {
			return nil, fmt.Errorf("yuanbao: sign token error code=%d", result.Code)
		}
		if result.Data == nil {
			return nil, fmt.Errorf("yuanbao: sign token response missing data")
		}
		return &tokenData{
			token: result.Data.Token, botID: result.Data.BotID,
			duration: result.Data.Duration, product: result.Data.Product,
			source: result.Data.Source,
		}, nil
	}
	return nil, lastErr

View on GitHub (pinned to 4000b2338a)

Solutions

  1. Log/print the raw respBody to see what was actually returned (HTML page? different JSON shape?).
  2. Confirm api_domain points at the genuine sign-token endpoint, not a proxy or different service.
  3. Check whether the yuanbao API version changed and update the response struct in platform/yuanbao/sign.go accordingly.
  4. If behind a corporate proxy, bypass it for this host so the real API response is received.
Defensive patterns

Strategy: type-guard

Validate before calling

if !json.Valid(respBody) { return fmt.Errorf("non-JSON response: %.200s", respBody) }

Type guard

func looksLikeTokenEnvelope(b []byte) bool {
    var probe struct{ Code *int `json:"code"` }
    return json.Unmarshal(b, &probe) == nil && probe.Code != nil
}

Try / catch

if err != nil && strings.Contains(err.Error(), "parse response") {
    slog.Error("unexpected sign-token body", "body", string(respBody))
}

Prevention

When it happens

Trigger: json.Unmarshal(respBody, &result) fails during a fetchToken attempt — empty body, HTML error page from a proxy, or a changed/re-versioned API response schema.

Common situations: A reverse proxy or captive portal returning an HTML login page with 200 status, api_domain pointing at a wrong service that returns different JSON, upstream API schema change after a version bump.

Understand the failure class

Background: "failed to unmarshal" / json.Unmarshal errors: why parsing a response into a Go struct fails and how to fix it — this error's family across 23 libraries.

Related errors


AI-assisted analysis of chenhg5/cc-connect@4000b2338a (2026-09-06). Data as JSON: /api/errors/31291899cf4d138d. Report an issue: GitHub.