chenhg5/cc-connect · info
googlechat: upload: encode metadata: %w
Error message
googlechat: upload: encode metadata: %w
What it means
Returned by uploadAttachment when json.Encoder.Encode fails while serializing the {"filename": ...} metadata map into the multipart metadata part. Because the encoder writes to an in-memory bytes.Buffer, Encode can only fail if the part writer is broken; with a plain map[string]string this is practically unreachable. It propagates through postAttachment from SendImage/SendFile.
Source
Thrown at platform/googlechat/googlechat.go:463
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)
if err != nil {
return "", fmt.Errorf("googlechat: upload: build request: %w", err)
}View on GitHub (pinned to 4000b2338a)
Solutions
- Confirm you are running unmodified library code; the payload is a trivial map.
- If seen after local changes, check that the metadata part writer is not already closed or broken.
- Check the error chain (%w) for the underlying cause and report a bug if it occurs on stock code.
Defensive patterns
Strategy: try-catch
Try / catch
if err := p.SendImage(ctx, target, path); err != nil {
if strings.Contains(err.Error(), "encode metadata") {
return fmt.Errorf("googlechat upload metadata failure: %w", err)
}
return err
} Prevention
- Run unmodified library code; the metadata payload is a fixed map.
- Keep library versions pinned and unpatched in this area.
- Include the full error chain in bug reports for diagnosis.
When it happens
Trigger: Calling SendImage()/SendFile(); during uploadAttachment the json.NewEncoder(metaPart).Encode(map[string]string{"filename": filename}) call returns an error — only possible if the underlying multipart part write fails.
Common situations: Effectively never hit in production with stock code; may appear if the multipart writer or buffer was modified or data was corrupted upstream.
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
- googlechat: upload: create metadata part: %w
- googlechat: upload: create media part: %w
- googlechat: upload: write media: %w
- googlechat: upload: finalize multipart body: %w
- googlechat: upload: decode response: %w
AI-assisted analysis of chenhg5/cc-connect@4000b2338a (2026-09-06).
Data as JSON: /api/errors/144985abe7b55fad.
Report an issue: GitHub.