{"record":{"id":"144985abe7b55fad","repo":"chenhg5/cc-connect","slug":"googlechat-upload-encode-metadata-w","errorCode":null,"errorMessage":"googlechat: upload: encode metadata: %w","messagePattern":"googlechat: upload: encode metadata: %w","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"info","filePath":"platform/googlechat/googlechat.go","lineNumber":463,"sourceCode":"\treturn p.post(ctx, rctx, content)\n}\n\nfunc (p *Platform) Send(ctx context.Context, rctx any, content string) error {\n\treturn p.post(ctx, rctx, content)\n}\n\n// uploadAttachment uploads raw bytes to the Chat media endpoint using a\n// 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}","sourceCodeStart":445,"sourceCodeEnd":481,"githubUrl":"https://github.com/chenhg5/cc-connect/blob/4000b2338aa6e850c99df54f8b0ed6ed7460b401/platform/googlechat/googlechat.go#L445-L481","documentation":"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.","triggerScenarios":"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.","commonSituations":"Effectively never hit in production with stock code; may appear if the multipart writer or buffer was modified or data was corrupted upstream.","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."],"exampleFix":null,"handlingStrategy":"try-catch","validationCode":null,"typeGuard":null,"tryCatchPattern":"if err := p.SendImage(ctx, target, path); err != nil {\n    if strings.Contains(err.Error(), \"encode metadata\") {\n        return fmt.Errorf(\"googlechat upload metadata failure: %w\", err)\n    }\n    return err\n}","preventionTips":["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."],"tags":["googlechat","upload","json","multipart"],"backgroundTag":"json-marshal-failed","analyzedSha":"4000b2338aa6e850c99df54f8b0ed6ed7460b401","analyzedAt":"2026-09-06T11:45:09.575Z","contentChangedAt":"2026-09-06T11:45:09.575Z","schemaVersion":2},"datasetVersion":"2026-09-14T05:17:10.506Z"}