{"record":{"id":"d1605a058b9693e4","repo":"siyuan-note/siyuan","slug":"invalid-block-structure-block-node-is-nil","errorCode":null,"errorMessage":"invalid block structure: block node is nil","messagePattern":"invalid block structure: block node is nil","errorType":"validation","errorClass":null,"httpStatus":null,"severity":"error","filePath":"kernel/treenode/block_structure.go","lineNumber":242,"sourceCode":"\t\titem.ListData.Num = num\n\t\titem.ListData.Delimiter = delimiter\n\t\titem.ListData.Marker = []byte(strconv.Itoa(num) + string(delimiter))\n\t\tnum++\n\t}\n}\n\nfunc isContentBlock(node *ast.Node) bool {\n\treturn nil != node && node.IsBlock() && ast.NodeKramdownBlockIAL != node.Type\n}\n\nfunc invalidBlockContainmentError(parent, child *ast.Node) error {\n\treturn fmt.Errorf(\"invalid block structure: %s [%s] cannot contain %s [%s]\",\n\t\tparent.Type.String(), parent.ID, child.Type.String(), child.ID)\n}\n\nfunc invalidBlockNodeError(node *ast.Node) error {\n\tif nil == node {\n\t\treturn fmt.Errorf(\"invalid block structure: block node is nil\")\n\t}\n\treturn fmt.Errorf(\"invalid block structure: %s [%s] is not a content block\", node.Type.String(), node.ID)\n}\n","sourceCodeStart":224,"sourceCodeEnd":246,"githubUrl":"https://github.com/siyuan-note/siyuan/blob/9f775e8a12daef8255556097396f9b2739078892/kernel/treenode/block_structure.go#L224-L246","documentation":"invalidBlockNodeError returns this message when the node handed to the block structure validator is nil. Since a nil AST node has no type or ID, the validator cannot classify it and reports a nil block node explicitly. It is thrown by ValidateBlockSubtree, ValidateBlockPlacement, and ValidateBlockReplacement as the first guard.","triggerScenarios":"Calling validation/insert/replace APIs with a node reference that failed lookup — e.g. an ID that does not exist, a tree that was not loaded, or passing nil explicitly after a failed query in calling code.","commonSituations":"Plugin code that assumes a block ID exists without checking; racing deletes where the node is removed between lookup and validation; parse failures leaving the node nil.","solutions":["Verify the block ID exists before validating (e.g. via a query of the blocks table)","Re-fetch the tree/node after any delete/rename operation; handle the not-found case instead of forwarding nil","Check that the tree was parsed successfully before operating on its nodes"],"exampleFix":"// before\nnode := tree.FindByID(id)\nreturn treenode.ValidateBlockPlacement(node, child) // node may be nil\n// after\nnode := tree.FindByID(id)\nif node == nil {\n    return fmt.Errorf(\"block %s not found\", id)\n}\nreturn treenode.ValidateBlockPlacement(node, child)","handlingStrategy":"type-guard","validationCode":"const info = await getBlockInfo(id);\nif (!info) throw new Error(\"block not found: \" + id);","typeGuard":"function nodeExists(n) { return n != null && typeof n.ID === \"string\" && n.ID.length > 0; }","tryCatchPattern":"try { treenode.ValidateBlockPlacement(node, child); } catch (e) { if (String(e.message).includes(\"block node is nil\")) reFetchTree(); else throw e; }","preventionTips":["Check that IDs resolve before calling validation/insert APIs","Re-fetch nodes after delete/sync operations","Never forward a nil node from a failed lookup; fail fast with your own clear error"],"tags":["document","null","validation"],"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"}