chenhg5/cc-connect · error

wecom: create form file: %w

Error message

wecom: create form file: %w

What it means

Wrap of multipart.Writer.CreateFormFile while building the image upload body in uploadImageMedia. Because the field name is the constant "media" and the destination is an in-memory bytes.Buffer, this error is practically unreachable — it is a defensive wrap required by the multipart API's error signature, not an expected runtime failure.

Source

Thrown at platform/wecom/wecom.go:554

	if result.ErrCode != 0 {
		return fmt.Errorf("wecom: send image failed: %d %s", result.ErrCode, result.ErrMsg)
	}
	return nil
}

// uploadImageMedia uploads an image to WeChat Work media API and returns the media_id.
func (p *Platform) uploadImageMedia(accessToken string, img core.ImageAttachment) (string, error) {
	name := img.FileName
	if name == "" {
		name = "image.png"
	}

	body := &bytes.Buffer{}
	writer := multipart.NewWriter(body)

	part, err := writer.CreateFormFile("media", name)
	if err != nil {
		return "", fmt.Errorf("wecom: create form file: %w", err)
	}
	if _, err := part.Write(img.Data); err != nil {
		return "", fmt.Errorf("wecom: write image data: %w", err)
	}
	if err := writer.Close(); err != nil {
		return "", fmt.Errorf("wecom: close multipart writer: %w", err)
	}

	apiURL := p.wecomAPIURL("/cgi-bin/media/upload", url.Values{
		"access_token": []string{accessToken},
		"type":         []string{"image"},
	})
	resp, err := p.apiClient.Post(apiURL, writer.FormDataContentType(), body)
	if err != nil {
		return "", fmt.Errorf("wecom: upload image: %w", err)
	}
	defer resp.Body.Close()

View on GitHub (pinned to 4000b2338a)

Solutions

  1. No action needed in normal operation; if it ever fires, log it and abort the upload
  2. The filename comes from img.FileName (defaulted to image.png); sanitize it if paranoid about header injection into the multipart part
Defensive patterns

Strategy: try-catch

When it happens

Trigger: Thrown at platform/wecom/wecom.go:554 when the library encounters an invalid state.

Common situations: See trigger scenarios.


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