chenhg5/cc-connect · error

dingtalk: marshal file message: %w

Error message

dingtalk: marshal file message: %w

What it means

SendFile failed to JSON-marshal the request body for the oToMessages batchSend call. This is nearly impossible in practice since the body is built from simple string fields, and would indicate an internal bug (e.g. a field type json cannot serialize, like a channel or func) or memory exhaustion.

Source

Thrown at platform/dingtalk/dingtalk.go:1142

	if idx := strings.LastIndex(name, "."); idx >= 0 {
		ext = name[idx+1:]
	}

	msgParamBytes, _ := json.Marshal(map[string]string{
		"mediaId":  mediaID,
		"fileName": name,
		"fileType": ext,
	})
	requestBody := map[string]any{
		"robotCode": p.robotCode,
		"userIds":   []string{rc.senderStaffId},
		"msgKey":    "sampleFile",
		"msgParam":  string(msgParamBytes),
	}

	body, err := json.Marshal(requestBody)
	if err != nil {
		return fmt.Errorf("dingtalk: marshal file message: %w", err)
	}

	req, err := http.NewRequestWithContext(ctx, http.MethodPost,
		"https://api.dingtalk.com/v1.0/robot/oToMessages/batchSend",
		bytes.NewReader(body))
	if err != nil {
		return fmt.Errorf("dingtalk: create file request: %w", err)
	}
	req.Header.Set("Content-Type", "application/json")
	req.Header.Set("x-acs-dingtalk-access-token", token)

	resp, err := p.httpClient.Do(req)
	if err != nil {
		return fmt.Errorf("dingtalk: send file request: %w", err)
	}
	defer func() { _ = resp.Body.Close() }()

	respBody, _ := io.ReadAll(resp.Body)

View on GitHub (pinned to 4000b2338a)

Solutions

  1. Inspect the struct being marshalled for json-unsupported types (chan, func, cyclic refs)
  2. Rebuild from a clean checkout to rule out local edits
  3. Check host memory availability if OOM symptoms accompany the error
  4. If you added fields to requestBody, ensure they are JSON-serializable types

Example fix

// before
requestBody["callback"] = someFunc // not JSON serializable
// after
requestBody["callbackName"] = callbackFuncName // string instead
Defensive patterns

Strategy: try-catch

Try / catch

if err := p.SendFile(ctx, rc, file); err != nil {
    if strings.Contains(err.Error(), "marshal file message") {
        // internal serialization bug — report with stack trace
        slog.Error("dingtalk: non-serializable request body", "err", err)
    }
    return err
}

Prevention

When it happens

Trigger: Internal struct contains an unserializable value; extreme low-memory conditions; corrupted build with a wrong field type in requestBody.

Common situations: Only seen after modifying the platform code and adding a non-serializable field to requestBody; otherwise effectively never.

Understand the failure class

Background: json.Marshal / "failed to marshal" errors in Go: why "unsupported type" happens and how to fix it — this error's family across 22 libraries.

Related errors


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