{"record":{"id":"1d0169af39d1744d","repo":"siyuan-note/siyuan","slug":"marshal-notebook-crypt-backup-failed-w","errorCode":null,"errorMessage":"marshal notebook crypt backup failed: %w","messagePattern":"marshal notebook crypt backup failed: %w","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"kernel/model/crypto.go","lineNumber":2533,"sourceCode":"\treturn filepath.Join(util.DataDir, boxID, \".siyuan\", notebookCryptoBackupFilename)\n}\n\n// writeNotebookCryptBackup 写入加密笔记本的 BoxCrypt 备份。\n// 仅在 Encrypted=true 的笔记本上调用，配合 CreateEncryptedBox / ChangeMasterPassword 写入。\nfunc writeNotebookCryptBackup(boxID string, crypt *conf.BoxEncryption) error {\n\tif !ast.IsNodeIDPattern(boxID) {\n\t\treturn errors.New(\"invalid notebook ID\")\n\t}\n\tif err := validateBoxEncryption(crypt); err != nil {\n\t\treturn err\n\t}\n\tbackupPath := notebookCryptoBackupPath(boxID)\n\tif err := os.MkdirAll(filepath.Dir(backupPath), 0755); err != nil {\n\t\treturn fmt.Errorf(\"mkdir notebook crypt backup dir failed: %w\", err)\n\t}\n\tdata, err := gulu.JSON.MarshalIndentJSON(crypt, \"\", \"  \")\n\tif err != nil {\n\t\treturn fmt.Errorf(\"marshal notebook crypt backup failed: %w\", err)\n\t}\n\tif err := filelock.WriteFile(backupPath, data); err != nil {\n\t\treturn fmt.Errorf(\"write notebook crypt backup failed: %w\", err)\n\t}\n\treturn nil\n}\n\n// readNotebookCryptBackup 读取加密笔记本的 BoxCrypt 备份。\n// 备份文件不存在时返回 (nil, nil)，调用方据此区分\"非加密笔记本\"和\"备份不存在\"。\nfunc readNotebookCryptBackup(boxID string) (*conf.BoxEncryption, error) {\n\tif !ast.IsNodeIDPattern(boxID) {\n\t\treturn nil, errors.New(\"invalid notebook ID\")\n\t}\n\tbackupPath := notebookCryptoBackupPath(boxID)\n\tif !filelock.IsExist(backupPath) {\n\t\treturn nil, nil\n\t}\n\treturn readBoxEncryptionFile(backupPath)","sourceCodeStart":2515,"sourceCodeEnd":2551,"githubUrl":"https://github.com/siyuan-note/siyuan/blob/8641553a1f07374001902d3ce773285db1292b2d/kernel/model/crypto.go#L2515-L2551","documentation":"After creating the backup directory, writeNotebookCryptBackup serializes the conf.BoxEncryption struct with gulu.JSON.MarshalIndentJSON. This wrapped error means that JSON marshaling failed, which is nearly impossible for a valid in-memory struct and usually signals an internal invariant problem (e.g. an unmarshalable value injected into the struct).","triggerScenarios":"gulu.JSON.MarshalIndentJSON(crypt, \"\", \"  \") returns an error while writing the backup in CreateEncryptedBox or ChangeMasterPassword — e.g. crypt contains a field that cannot be serialized (channel/func value) due to a code defect or corrupted conf object.","commonSituations":"A plugin or patched build placing unexpected values in BoxEncryption; version drift where conf.BoxEncryption gained a non-serializable field; memory corruption is effectively ruled out.","solutions":["Inspect the wrapped error to identify which field fails to marshal","Verify conf.BoxEncryption only contains plain serializable fields (strings/bytes-as-base64); fix or upgrade the code that populated it","Re-run CreateEncryptedBox / ChangeMasterPassword with a freshly constructed BoxEncryption","Report as a kernel bug if a stock configuration reproduces it"],"exampleFix":null,"handlingStrategy":"try-catch","validationCode":null,"typeGuard":null,"tryCatchPattern":"if err := model.CreateEncryptedBox(box, password); err != nil {\n    if strings.Contains(err.Error(), \"marshal notebook crypt backup\") {\n        // internal invariant broken: report bug, do not retry blindly\n        return reportKernelBug(err)\n    }\n    return err\n}","preventionTips":["Keep conf.BoxEncryption limited to serializable field types","Upgrade the kernel as one unit — avoid mixing conf structs across versions"],"tags":["json","serialization","backup","encryption"],"backgroundTag":"json-marshal-failed","analyzedSha":"8641553a1f07374001902d3ce773285db1292b2d","analyzedAt":"2026-09-11T16:08:28.414Z","contentChangedAt":"2026-09-11T16:08:28.414Z","schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}