chenhg5/cc-connect · info

googlechat: upload: create metadata part: %w

Error message

googlechat: upload: create metadata part: %w

What it means

Returned by uploadAttachment when multipart.Writer.CreatePart fails while creating the JSON metadata part of a multipart/related upload body. CreatePart only fails if the MIME header is invalid; since the header here is a hardcoded constant, this error is practically unreachable and indicates an internal invariant violation. It is wrapped with googlechat: context and surfaces via postAttachment (SendImage/SendFile).

Source

Thrown at platform/googlechat/googlechat.go:460

}

func (p *Platform) Reply(ctx context.Context, rctx any, content string) error {
	return p.post(ctx, rctx, content)
}

func (p *Platform) Send(ctx context.Context, rctx any, content string) error {
	return p.post(ctx, rctx, content)
}

// uploadAttachment uploads raw bytes to the Chat media endpoint using a
// 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)

View on GitHub (pinned to 4000b2338a)

Solutions

  1. Verify you are on an unmodified version of the library; this is a hardcoded valid header.
  2. If reproducible after local modification, inspect the MIME header passed to CreatePart for invalid characters or empty keys.
  3. Report as a bug with the full error chain if it occurs on stock code.
Defensive patterns

Strategy: try-catch

Try / catch

name, err := p.SendFile(ctx, target, file)
if err != nil {
    if strings.Contains(err.Error(), "create metadata part") {
        // internal invariant violation; not caller-recoverable
        return fmt.Errorf("library bug: %w", err)
    }
    return err
}

Prevention

When it happens

Trigger: Calling SendImage() or SendFile(); during multipart body construction the CreatePart call for the application/json metadata part returns an error — essentially only if the textproto.MIMEHeader constant were malformed.

Common situations: Effectively never hit in production; would only appear after a code change that builds the header dynamically with invalid header names/values.

Related errors


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