{"record":{"id":"5155cc8615da61f6","repo":"chenhg5/cc-connect","slug":"googlechat-upload-finalize-multipart-body-w","errorCode":null,"errorMessage":"googlechat: upload: finalize multipart body: %w","messagePattern":"googlechat: upload: finalize multipart body: %w","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"info","filePath":"platform/googlechat/googlechat.go","lineNumber":474,"sourceCode":"\tmw := multipart.NewWriter(buf)\n\n\tmetaPart, err := mw.CreatePart(textproto.MIMEHeader{\"Content-Type\": {\"application/json; charset=UTF-8\"}})\n\tif err != nil {\n\t\treturn \"\", fmt.Errorf(\"googlechat: upload: create metadata part: %w\", err)\n\t}\n\tif err := json.NewEncoder(metaPart).Encode(map[string]string{\"filename\": filename}); err != nil {\n\t\treturn \"\", fmt.Errorf(\"googlechat: upload: encode metadata: %w\", err)\n\t}\n\n\tmediaPart, err := mw.CreatePart(textproto.MIMEHeader{\"Content-Type\": {mimeType}})\n\tif err != nil {\n\t\treturn \"\", fmt.Errorf(\"googlechat: upload: create media part: %w\", err)\n\t}\n\tif _, err := mediaPart.Write(data); err != nil {\n\t\treturn \"\", fmt.Errorf(\"googlechat: upload: write media: %w\", err)\n\t}\n\tif err := mw.Close(); err != nil {\n\t\treturn \"\", fmt.Errorf(\"googlechat: upload: finalize multipart body: %w\", err)\n\t}\n\n\tuploadURL := chatUploadBase + space + \"/attachments:upload?uploadType=multipart\"\n\treq, err := http.NewRequestWithContext(ctx, http.MethodPost, uploadURL, buf)\n\tif err != nil {\n\t\treturn \"\", fmt.Errorf(\"googlechat: upload: build request: %w\", err)\n\t}\n\treq.Header.Set(\"Content-Type\", \"multipart/related; boundary=\"+mw.Boundary())\n\n\tresp, err := p.doRequest(req)\n\tif err != nil {\n\t\treturn \"\", err\n\t}\n\tdefer func() {\n\t\tif err := resp.Body.Close(); err != nil {\n\t\t\tslog.Warn(\"googlechat: close upload response body\", \"error\", err)\n\t\t}\n\t}()","sourceCodeStart":456,"sourceCodeEnd":492,"githubUrl":"https://github.com/chenhg5/cc-connect/blob/4000b2338aa6e850c99df54f8b0ed6ed7460b401/platform/googlechat/googlechat.go#L456-L492","documentation":"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.","triggerScenarios":"Calling SendImage()/SendFile(); mw.Close() returns an error during multipart body finalization — only possible with corrupted writer state or local modifications.","commonSituations":"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).","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."],"exampleFix":null,"handlingStrategy":"try-catch","validationCode":null,"typeGuard":null,"tryCatchPattern":"if err := p.SendFile(ctx, target, file); err != nil {\n    if strings.Contains(err.Error(), \"finalize multipart body\") {\n        return fmt.Errorf(\"googlechat multipart finalize failure (library bug): %w\", err)\n    }\n    return err\n}","preventionTips":["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."],"tags":["googlechat","upload","multipart","unreachable"],"backgroundTag":"multipart-write-failed","analyzedSha":"4000b2338aa6e850c99df54f8b0ed6ed7460b401","analyzedAt":"2026-09-06T11:45:09.575Z","contentChangedAt":"2026-09-06T11:45:09.575Z","schemaVersion":2},"datasetVersion":"2026-09-14T00:17:10.932Z"}