{"record":{"id":"8d92c35e1c3e3c46","repo":"larksuite/cli","slug":"cannot-rewrite-non-body-media-type-q","errorCode":null,"errorMessage":"cannot rewrite non-body media type %q","messagePattern":"cannot rewrite non-body media type %q","errorType":"validation","errorClass":null,"httpStatus":null,"severity":"error","filePath":"shortcuts/mail/draft/patch.go","lineNumber":478,"sourceCode":"}\n\nfunc ensureBodyPartRef(partRef **Part, bodyKind string) (*Part, error) {\n\tif partRef == nil {\n\t\treturn nil, fmt.Errorf(\"body container is nil\")\n\t}\n\tif *partRef == nil {\n\t\tleaf := newBodyLeaf(bodyKind)\n\t\tleaf.Dirty = true\n\t\t*partRef = leaf\n\t\treturn leaf, nil\n\t}\n\tpart := *partRef\n\tif !part.IsMultipart() {\n\t\tif strings.EqualFold(part.MediaType, bodyKind) {\n\t\t\treturn part, nil\n\t\t}\n\t\tif !isBodyKind(part.MediaType) {\n\t\t\treturn nil, fmt.Errorf(\"cannot rewrite non-body media type %q\", part.MediaType)\n\t\t}\n\t\tnewLeaf := newBodyLeaf(bodyKind)\n\t\talt := newMultipartContainer(\"multipart/alternative\")\n\t\tif strings.EqualFold(part.MediaType, \"text/plain\") {\n\t\t\talt.Children = []*Part{part, newLeaf}\n\t\t} else {\n\t\t\talt.Children = []*Part{newLeaf, part}\n\t\t}\n\t\talt.Dirty = true\n\t\tnewLeaf.Dirty = true\n\t\t*partRef = alt\n\t\treturn newLeaf, nil\n\t}\n\n\tswitch strings.ToLower(part.MediaType) {\n\tcase \"multipart/alternative\":\n\t\tfor _, child := range part.Children {\n\t\t\tif child != nil && strings.EqualFold(child.MediaType, bodyKind) {","sourceCodeStart":460,"sourceCodeEnd":496,"githubUrl":"https://github.com/larksuite/cli/blob/7fd6ef3c07182257ce776cdc5a614e122d5bd4b3/shortcuts/mail/draft/patch.go#L460-L496","documentation":"The draft's primary body slot holds a non-multipart part whose media type is neither text/plain, text/html, nor equal to the requested bodyKind (isBodyKind fails). The library cannot safely convert such content (e.g. application/pdf, image/*, text/calendar) into a text body, so it refuses the rewrite rather than destroying the existing part. This protects drafts whose primary content is not rewriteable text.","triggerScenarios":"Rewriting the body of a draft whose root body part has a non-body media type — e.g. a draft consisting of a single attachment-like part (application/octet-stream, image/png) or a non-MIME content type — then calling a body rewrite targeting text/plain or text/html.","commonSituations":"Drafts imported from external systems with odd root MIME structures; snapshots where the body slot was overwritten with attachment content; automation that assumes the root is always text or multipart.","solutions":["Inspect snapshot.Body.MediaType before rewriting and wrap non-body roots into a multipart/mixed container (with the real content as one child and the body as another) using the attachment path.","Add the attachment via addAttachment instead of placing it as the root body, so the root stays a body kind and rewrite works.","Rebuild the draft with a proper body structure if the source system produced a non-body root."],"exampleFix":"// before: attachment stored as root body\nsnapshot.Body = pdfPart\nerr := ensureBodyPart(snapshot, \"text/html\") // fails\n// after: proper structure\nsnapshot.Body = textLeaf\naddAttachment(ctx, snapshot, \"report.pdf\")\nerr := ensureBodyPart(snapshot, \"text/html\")","handlingStrategy":"validation","validationCode":"if snapshot.Body != nil && !snapshot.Body.IsMultipart() && !isBodyKind(snapshot.Body.MediaType) {\n\t// root is non-body content: restructure or use attachment flow first\n\taddAttachment(ctx, snapshot, filePath)\n\tsnapshot.Body = newBodyLeaf(\"text/plain\")\n}","typeGuard":"func bodyRewritable(p *Part) bool {\n\treturn p != nil && (p.IsMultipart() || isBodyKind(p.MediaType))\n}","tryCatchPattern":null,"preventionTips":["Keep non-text content out of the root body slot; use addAttachment for files.","Check snapshot.Body.MediaType before rewriting.","Rebuild drafts with pathological MIME roots rather than patching them in place."],"tags":["email","mime","draft","unsupported-content"],"backgroundTag":"unsupported-mime-rewrite","analyzedSha":"7fd6ef3c07182257ce776cdc5a614e122d5bd4b3","analyzedAt":"2026-09-04T21:17:44.649Z","contentChangedAt":"2026-09-04T21:17:44.649Z","schemaVersion":2},"datasetVersion":"2026-09-12T02:17:10.037Z"}