{"record":{"id":"9f639d20ebfe36e6","repo":"siyuan-note/siyuan","slug":"block-write-failed-empty-block-id","errorCode":null,"errorMessage":"block write failed: empty block ID","messagePattern":"block write failed: empty block ID","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"kernel/cli/cmd/block.go","lineNumber":321,"sourceCode":"\t\t\tParentID: parentID,\n\t\t}\n\t\tif err = model.PerformTxSync(&model.Transaction{DoOperations: []*model.Operation{operation}}); err != nil {\n\t\t\treturn err\n\t\t}\n\t\tif bt := treenode.GetBlockTree(parentID); bt != nil {\n\t\t\tmodel.AppendPushReloadProtyleEntry(bt.RootID)\n\t\t}\n\t\treturn printBlockWriteResult(operation.ID)\n\t},\n}\n\nfunc printBlockWriteResult(id string) error {\n\treturn writeBlockWriteResult(os.Stdout, id)\n}\n\nfunc writeBlockWriteResult(output io.Writer, id string) error {\n\tif id == \"\" {\n\t\treturn fmt.Errorf(\"block write failed: empty block ID\")\n\t}\n\tif outputFormat == \"json\" {\n\t\tdata, err := json.MarshalIndent(map[string]string{\"id\": id}, \"\", \"  \")\n\t\tif err != nil {\n\t\t\treturn err\n\t\t}\n\t\t_, err = fmt.Fprintln(output, string(data))\n\t\treturn err\n\t}\n\t_, err := fmt.Fprintln(output, id)\n\treturn err\n}\n\nvar blockUpdateCmd = &cobra.Command{\n\tUse:   \"update --id <id> [--data <markdown> | --file <path>]\",\n\tShort: \"Update block\",\n\tRunE: func(cmd *cobra.Command, args []string) error {\n\t\tid, _ := cmd.Flags().GetString(\"id\")","sourceCodeStart":303,"sourceCodeEnd":339,"githubUrl":"https://github.com/siyuan-note/siyuan/blob/9f775e8a12daef8255556097396f9b2739078892/kernel/cli/cmd/block.go#L303-L339","documentation":"Helper writeBlockWriteResult refuses to print a block-write result when the resulting block ID is empty, treating it as evidence that the write did not produce a usable block. This guards against reporting a successful write with no identifier.","triggerScenarios":"Calling printBlockWriteResult (or writeBlockWriteResult directly, as tests do) with id == \"\" after a block insert/update/append/prepend operation whose returned ID was empty.","commonSituations":"Upstream write API returned success but no ID (silent failure); caller forgot to check the write error before formatting output; test verifying the empty-ID rejection path.","solutions":["Check the error from the underlying block write before calling printBlockWriteResult.","Ensure the write operation returns a non-empty block ID on success; if it doesn't, fail earlier with the real cause.","In tests, pass a non-empty ID (or keep the empty case as an explicit rejection test)."],"exampleFix":"// before\nerr := doBlockWrite(...)\nprintBlockWriteResult(newID) // newID may be \"\"\n// after\nif err := doBlockWrite(...); err != nil {\n    return err\n}\nif newID == \"\" {\n    return fmt.Errorf(\"block write returned no ID\")\n}\nreturn printBlockWriteResult(newID)","handlingStrategy":"try-catch","validationCode":"if newID == \"\" {\n    return fmt.Errorf(\"block write returned no ID\")\n}\nerr := printBlockWriteResult(newID)","typeGuard":"func hasBlockID(id string) bool { return id != \"\" }","tryCatchPattern":"if err := printBlockWriteResult(id); err != nil {\n    return fmt.Errorf(\"report block write result: %w\", err)\n}","preventionTips":["Check the write operation's error before printing its result","Treat an empty returned ID as a failed write and surface the real cause","Cover the empty-ID rejection path in tests"],"tags":["cli","empty-id","validation"],"backgroundTag":"empty-required-field","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"}