{"record":{"id":"41bdf4c441371a99","repo":"siyuan-note/siyuan","slug":"document-sort-item-must-not-be-nil","errorCode":null,"errorMessage":"document sort item must not be nil","messagePattern":"document sort item must not be nil","errorType":"validation","errorClass":null,"httpStatus":null,"severity":"error","filePath":"kernel/model/file.go","lineNumber":2927,"sourceCode":"\t\t\treturn ret, errors.New(\"notebook sort item must not be nil\")\n\t\t}\n\t\tif _, ok := notebookIDs[item.ID]; ok {\n\t\t\treturn ret, fmt.Errorf(\"duplicate notebook ID [%s]\", item.ID)\n\t\t}\n\t\tnotebookIDs[item.ID] = struct{}{}\n\n\t\tbox := boxes[item.ID]\n\t\tif nil == box {\n\t\t\treturn ret, fmt.Errorf(\"notebook [%s] not found\", item.ID)\n\t\t}\n\t\tnotebookPlans = append(notebookPlans, &notebookSortPlan{item: item, box: box})\n\t}\n\n\tdocPlans := make([]*docSortPlan, 0, len(docSorts))\n\tdocIDs := map[string]struct{}{}\n\tfor _, item := range docSorts {\n\t\tif nil == item {\n\t\t\treturn ret, errors.New(\"document sort item must not be nil\")\n\t\t}\n\t\tif _, ok := docIDs[item.ID]; ok {\n\t\t\treturn ret, fmt.Errorf(\"duplicate document ID [%s]\", item.ID)\n\t\t}\n\t\tdocIDs[item.ID] = struct{}{}\n\n\t\tbt := treenode.GetBlockTree(item.ID)\n\t\tif nil == bt || nil == openedBoxes[bt.BoxID] {\n\t\t\treturn ret, fmt.Errorf(\"document [%s] not found in opened and unlocked notebooks\", item.ID)\n\t\t}\n\t\tif bt.ID != bt.RootID || \"d\" != bt.Type || IsBoxDoc(bt.BoxID, bt.RootID) {\n\t\t\treturn ret, fmt.Errorf(\"block [%s] is not a sortable document\", item.ID)\n\t\t}\n\t\tif nil == boxes[bt.BoxID] {\n\t\t\treturn ret, fmt.Errorf(\"notebook [%s] not found for document [%s]\", bt.BoxID, item.ID)\n\t\t}\n\t\tdocPlans = append(docPlans, &docSortPlan{item: item, boxID: bt.BoxID, parentPath: path.Dir(bt.Path)})\n\t}","sourceCodeStart":2909,"sourceCodeEnd":2945,"githubUrl":"https://github.com/siyuan-note/siyuan/blob/9f775e8a12daef8255556097396f9b2739078892/kernel/model/file.go#L2909-L2945","documentation":"Document sort planning requires every entry of docSorts to be a non-nil item; a nil entry aborts with errors.New(\"document sort item must not be nil\"). Like the notebook check (error 1393), it is a request-shape guard ensuring the payload contains only concrete sort items before any tree mutation.","triggerScenarios":"Posting the document sort API with null elements inside the docSorts array — unassigned Go slice slots, JSON nulls from undefined array positions, or template-generated payloads with empty optional entries.","commonSituations":"Plugin builds make([]*DocSortItem, n) and fills only some indexes; frontend state array containing nulls after removals without compaction; scripting languages serializing undefined as null.","solutions":["Strip null/undefined entries from the docSorts array before the request","Initialize every element of the client-side array before serialization","Validate the payload schema client-side prior to sending"],"exampleFix":"// before: JS array with holes\nconst sorts = new Array(n); sorts[0] = {id: \"...\"};\nsortDocs(sorts)\n// after\nconst sorts = docs.map(d => ({id: d.id}));\nsortDocs(sorts.filter(Boolean))","handlingStrategy":"validation","validationCode":"if (docSorts.some(i => i == null)) throw new Error('docSorts contains null entries');","typeGuard":"const isDocSortItem = (i) => i != null && typeof i.id === 'string';","tryCatchPattern":"try { await api.sortDocs(docSorts); } catch (e) { if (String(e).includes('must not be nil')) { await api.sortDocs(docSorts.filter(Boolean)); } else { throw e; } }","preventionTips":["Compact arrays after element removal (filter(Boolean))","Initialize every slice/slot element before marshaling","Schema-validate request bodies in CI tests"],"tags":["go","null-check","request-validation","api"],"backgroundTag":"null-argument","analyzedSha":"9f775e8a12daef8255556097396f9b2739078892","analyzedAt":"2026-09-19T03:17:15.984Z","contentChangedAt":"2026-09-19T03:17:15.984Z","schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}