chenhg5/cc-connect · info
googlechat: upload: finalize multipart body: %w
Error message
googlechat: upload: finalize multipart body: %w
What it means
Returned by uploadAttachment when multipart.Writer.Close() fails while finalizing the multipart/related body. Close writes the terminating boundary; with an in-memory bytes.Buffer target this is practically unreachable with stock code. It propagates through postAttachment from SendImage/SendFile.
Source
Thrown at platform/googlechat/googlechat.go:474
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 {
slog.Warn("googlechat: close upload response body", "error", err)
}
}()View on GitHub (pinned to 4000b2338a)
Solutions
- Confirm unmodified library code; closing over a bytes.Buffer cannot fail.
- If seen after local changes, ensure no calls after Close that mutate writer state.
- Report a bug with the wrapped cause 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(), "finalize multipart body") {
return fmt.Errorf("googlechat multipart finalize failure (library bug): %w", err)
}
return err
} Prevention
- Do not interleave other multipart.Writer calls around the library's upload path.
- Report occurrences upstream as bugs.
- Keep library code unmodified in this area.
When it happens
Trigger: Calling SendImage()/SendFile(); mw.Close() returns an error during multipart body finalization — only possible with corrupted writer state or local modifications.
Common situations: Effectively never hit in production; would appear only if the multipart writer was misused in modified code (e.g. calls after Close that mutate writer state).
Related errors
- googlechat: upload: create metadata part: %w
- googlechat: upload: write media: %w
- googlechat: upload: encode metadata: %w
- googlechat: upload: create media part: %w
- create form file: %w
AI-assisted analysis of chenhg5/cc-connect@4000b2338a (2026-09-06).
Data as JSON: /api/errors/5155cc8615da61f6.
Report an issue: GitHub.