{"record":{"id":"7fe3e6b72aecf819","repo":"larksuite/cli","slug":"body-part-s-not-found","errorCode":null,"errorMessage":"body part %s not found","messagePattern":"body part (.+?) not found","errorType":"validation","errorClass":null,"httpStatus":null,"severity":"error","filePath":"shortcuts/mail/draft/patch.go","lineNumber":428,"sourceCode":"func bodyPartForKind(snapshot *DraftSnapshot, bodyKind string, allowRewrite bool) (*Part, error) {\n\tvar partID string\n\tswitch strings.ToLower(bodyKind) {\n\tcase \"text/plain\":\n\t\tpartID = snapshot.PrimaryTextPartID\n\tcase \"text/html\":\n\t\tpartID = snapshot.PrimaryHTMLPartID\n\tdefault:\n\t\treturn nil, fmt.Errorf(\"unsupported body kind %q\", bodyKind)\n\t}\n\tif partID == \"\" {\n\t\tif !allowRewrite {\n\t\t\treturn nil, fmt.Errorf(\"draft has no primary %s body part\", bodyKind)\n\t\t}\n\t\treturn ensureBodyPart(snapshot, bodyKind)\n\t}\n\tpart := findPart(snapshot.Body, partID)\n\tif part == nil {\n\t\treturn nil, fmt.Errorf(\"body part %s not found\", partID)\n\t}\n\treturn part, nil\n}\n\nfunc ensureBodyPart(snapshot *DraftSnapshot, bodyKind string) (*Part, error) {\n\tpartRef := primaryBodyRootRef(&snapshot.Body)\n\tif partRef == nil {\n\t\treturn nil, fmt.Errorf(\"draft has no primary body container\")\n\t}\n\treturn ensureBodyPartRef(partRef, bodyKind)\n}\n\nfunc primaryBodyRootRef(root **Part) **Part {\n\tif root == nil || *root == nil {\n\t\treturn root\n\t}\n\tpart := *root\n\tif strings.EqualFold(part.MediaType, \"multipart/mixed\") {","sourceCodeStart":410,"sourceCodeEnd":446,"githubUrl":"https://github.com/larksuite/cli/blob/7fd6ef3c07182257ce776cdc5a614e122d5bd4b3/shortcuts/mail/draft/patch.go#L410-L446","documentation":"The snapshot records a primary part ID for the requested body kind, but findPart cannot locate that part anywhere in the draft's MIME tree. This means the snapshot's part registry (PrimaryTextPartID/PrimaryHTMLPartID) is out of sync with the actual Part tree — a stale or internally inconsistent snapshot. The library refuses to patch a dangling reference rather than patching the wrong node.","triggerScenarios":"Calling replaceBody or appendBody with a snapshot where PrimaryTextPartID or PrimaryHTMLPartID points to a part ID absent from snapshot.Body — e.g. after external mutation of the tree, a partially-failed earlier patch, or a snapshot built from a MIME message where the referenced leaf was dropped.","commonSituations":"Reusing a DraftSnapshot parsed from an older version of the draft after the body was restructured; custom code that removed/replaced parts without updating primary IDs; bugs in downstream MIME rebuilds that orphan the primary leaf.","solutions":["Re-fetch/re-parse the draft to obtain a fresh, consistent DraftSnapshot before patching.","Fix the mutation code so any tree restructuring updates PrimaryTextPartID/PrimaryHTMLPartID (or leaves them intact).","Clear the stale primary ID (set it to \"\") so the draft goes through the ensureBodyPart path with allowRewrite instead of dangling."],"exampleFix":"// before: stale snapshot reused after body restructure\npart, err := bodyPartForKind(staleSnapshot, \"text/html\", false)\n// after: rebuild snapshot from current draft\nsnapshot, err := parseDraftSnapshot(currentDraft)\npart, err := bodyPartForKind(snapshot, \"text/html\", false)","handlingStrategy":"validation","validationCode":"if snapshot.PrimaryHTMLPartID != \"\" && findPart(snapshot.Body, snapshot.PrimaryHTMLPartID) == nil {\n\t// stale reference: re-fetch the draft and rebuild the snapshot\n\tsnapshot = reparseDraft(latestDraft)\n}","typeGuard":"func primaryPartResolvable(s *DraftSnapshot, kind string) bool {\n\tid := s.PrimaryTextPartID\n\tif strings.EqualFold(kind, \"text/html\") { id = s.PrimaryHTMLPartID }\n\treturn id == \"\" || findPart(s.Body, id) != nil\n}","tryCatchPattern":null,"preventionTips":["Never reuse a DraftSnapshot after the draft body was restructured externally.","Always update primary part IDs when mutating the MIME tree.","Re-parse the draft from its current state before each patch round."],"tags":["email","mime","draft","state-inconsistency"],"backgroundTag":"stale-reference-not-found","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"}