chenhg5/cc-connect · info

googlechat: upload: write media: %w

Error message

googlechat: upload: write media: %w

What it means

Returned by uploadAttachment when writing the file bytes to the multipart media part fails. The part writes to an in-memory bytes.Buffer which never fails on Write, so this error is practically unreachable with stock code. It is wrapped with googlechat: context and surfaces via postAttachment from SendImage/SendFile.

Source

Thrown at platform/googlechat/googlechat.go:471

// multipart/related request and returns the attachmentDataRef resource name.
func (p *Platform) uploadAttachment(ctx context.Context, space, filename, mimeType string, data []byte) (string, error) {
	buf := bytes.NewBuffer(make([]byte, 0, 256+len(data)))
	mw := multipart.NewWriter(buf)

	metaPart, err := mw.CreatePart(textproto.MIMEHeader{"Content-Type": {"application/json; charset=UTF-8"}})
	if err != nil {
		return "", fmt.Errorf("googlechat: upload: create metadata part: %w", err)
	}
	if err := json.NewEncoder(metaPart).Encode(map[string]string{"filename": filename}); err != nil {
		return "", fmt.Errorf("googlechat: upload: encode metadata: %w", err)
	}

	mediaPart, err := mw.CreatePart(textproto.MIMEHeader{"Content-Type": {mimeType}})
	if err != nil {
		return "", fmt.Errorf("googlechat: upload: create media part: %w", err)
	}
	if _, err := mediaPart.Write(data); err != nil {
		return "", fmt.Errorf("googlechat: upload: write media: %w", err)
	}
	if err := mw.Close(); err != nil {
		return "", fmt.Errorf("googlechat: upload: finalize multipart body: %w", err)
	}

	uploadURL := chatUploadBase + space + "/attachments:upload?uploadType=multipart"
	req, err := http.NewRequestWithContext(ctx, http.MethodPost, uploadURL, buf)
	if err != nil {
		return "", fmt.Errorf("googlechat: upload: build request: %w", err)
	}
	req.Header.Set("Content-Type", "multipart/related; boundary="+mw.Boundary())

	resp, err := p.doRequest(req)
	if err != nil {
		return "", err
	}
	defer func() {
		if err := resp.Body.Close(); err != nil {

View on GitHub (pinned to 4000b2338a)

Solutions

  1. Confirm unmodified library code; bytes.Buffer writes cannot fail.
  2. If seen after local changes, check ordering: CreatePart errors must abort before Write.
  3. Report a bug with the error chain if reproducible on stock code.
Defensive patterns

Strategy: try-catch

Try / catch

if err := p.SendFile(ctx, target, file); err != nil {
    if strings.Contains(err.Error(), "write media") {
        return fmt.Errorf("googlechat media write failure (library bug): %w", err)
    }
    return err
}

Prevention

When it happens

Trigger: Calling SendImage()/SendFile(); mediaPart.Write(data) returns an error — only possible if the multipart writer state was corrupted (e.g. after a prior failed CreatePart in modified code).

Common situations: Effectively never hit in production; would indicate corrupted writer state after local modifications.

Related errors


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