{"record":{"id":"adeb179a185d798d","repo":"siyuan-note/siyuan","slug":"notebook-sort-item-must-not-be-nil","errorCode":null,"errorMessage":"notebook sort item must not be nil","messagePattern":"notebook sort item must not be nil","errorType":"validation","errorClass":null,"httpStatus":null,"severity":"error","filePath":"kernel/model/file.go","lineNumber":2909,"sourceCode":"\t}\n\n\tFlushTxQueue()\n\tfileTreeSortLock.Lock()\n\tdefer fileTreeSortLock.Unlock()\n\tboxes := map[string]*Box{}\n\tfor _, box := range Conf.GetBoxes() {\n\t\tboxes[box.ID] = box\n\t}\n\topenedBoxes := map[string]*Box{}\n\tfor _, box := range Conf.GetOpenedBoxes() {\n\t\topenedBoxes[box.ID] = box\n\t}\n\n\tnotebookPlans := make([]*notebookSortPlan, 0, len(notebookSorts))\n\tnotebookIDs := map[string]struct{}{}\n\tfor _, item := range notebookSorts {\n\t\tif nil == item {\n\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\")","sourceCodeStart":2891,"sourceCodeEnd":2927,"githubUrl":"https://github.com/siyuan-note/siyuan/blob/9f775e8a12daef8255556097396f9b2739078892/kernel/model/file.go#L2891-L2927","documentation":"When planning a notebook sort, each entry in the notebookSorts request list must be a non-nil item. A nil entry means the request payload contained a null element in the sort array; the validation aborts with errors.New(\"notebook sort item must not be nil\") before any change is persisted. It is a defensive request-shape check.","triggerScenarios":"POSTing the sort API with a JSON body like {\"notebookSorts\":[null]} or [null,{...}] — usually produced by programmatic clients, plugins, or scripting that builds the array with unassigned slots.","commonSituations":"Plugin/script allocating a slice with make([]*Item, n) and leaving holes; JSON deserialization producing null for undefined entries; template-driven request generation with optional entries left empty.","solutions":["Remove null entries from the notebookSorts array before sending the request","In client code, filter or initialize all slice elements before marshaling to JSON","Validate the request payload shape against the API schema before submission"],"exampleFix":"// before: nil holes in Go client\nitems := make([]*SortItem, 3)\nitems[0] = &SortItem{ID: \"202401...\"}\nsortNotebooks(items)\n// after\nitems := make([]*SortItem, 0, 3)\nfor _, it := range rawItems {\n    if it != nil {\n        items = append(items, it)\n    }\n}\nsortNotebooks(items)","handlingStrategy":"validation","validationCode":"if (notebookSorts.some(i => i == null)) throw new Error('notebookSorts contains null entries');","typeGuard":"const isSortItem = (i) => i != null && typeof i.id === 'string' && i.id.length > 0;","tryCatchPattern":"try { await api.sortNotebooks(notebookSorts); } catch (e) { if (String(e).includes('must not be nil')) { await api.sortNotebooks(notebookSorts.filter(Boolean)); } else { throw e; } }","preventionTips":["Filter null/undefined from arrays before JSON serialization","Use push instead of index assignment when building sort lists","Validate payloads against the API schema in 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"}