{"record":{"id":"6d989958ee19f7b4","repo":"chenhg5/cc-connect","slug":"googlechat-upload-write-media-w","errorCode":null,"errorMessage":"googlechat: upload: write media: %w","messagePattern":"googlechat: upload: write media: %w","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"info","filePath":"platform/googlechat/googlechat.go","lineNumber":471,"sourceCode":"// multipart/related request and returns the attachmentDataRef resource name.\nfunc (p *Platform) uploadAttachment(ctx context.Context, space, filename, mimeType string, data []byte) (string, error) {\n\tbuf := bytes.NewBuffer(make([]byte, 0, 256+len(data)))\n\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 {","sourceCodeStart":453,"sourceCodeEnd":489,"githubUrl":"https://github.com/chenhg5/cc-connect/blob/4000b2338aa6e850c99df54f8b0ed6ed7460b401/platform/googlechat/googlechat.go#L453-L489","documentation":"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.","triggerScenarios":"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).","commonSituations":"Effectively never hit in production; would indicate corrupted writer state after local modifications.","solutions":["Confirm unmodified library code; bytes.Buffer writes cannot fail.","If seen after local changes, check ordering: CreatePart errors must abort before Write.","Report a bug with the error chain 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(), \"write media\") {\n        return fmt.Errorf(\"googlechat media write failure (library bug): %w\", err)\n    }\n    return err\n}","preventionTips":["Run unmodified library code; in-memory part writes cannot fail.","Report occurrences upstream as bugs.","Pin library versions to avoid locally introduced writer-state corruption."],"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"}