siyuan-note/siyuan · error

marshal notebook crypto backup failed

Error message

marshal notebook crypto backup failed: %w

What it means

saveNotebookCryptoBackup wraps a json.Marshal failure of the NotebookCrypto backup struct as "marshal notebook crypto backup failed: %w". json.Marshal of a plain configuration struct essentially never fails at runtime, so this indicates an internal invariant problem — e.g. an unsupported value type slipped into the struct (channel/func field, or a custom MarshalJSON returning an error) rather than any user-actionable condition.

Solutions

  1. Inspect the wrapped error to identify the offending field
  2. Check the recent diff on conf.NotebookCrypto for newly added fields and ensure they are JSON-serializable (add json:"-" tags where appropriate)
  3. Verify any custom MarshalJSON methods on the struct's fields return valid output for all states
  4. Run the crypto backup tests (kernel) to reproduce and confirm the fix

Example fix

// before
type NotebookCrypto struct {
    Callback func() `json:"callback"` // unsupported by json.Marshal
}
// after
type NotebookCrypto struct {
    callback func() // unexported, not serialized
}
Defensive patterns

Strategy: try-catch

Validate before calling

// Compile-time-ish sanity: ensure the struct round-trips in a startup self-test
if _, err := json.Marshal(*Conf.NotebookCrypto); err != nil {
    log.Fatalf("NotebookCrypto not serializable: %v", err)
}

Type guard

null

Try / catch

if err := saveNotebookCryptoBackup(kek); err != nil {
    if strings.Contains(err.Error(), "marshal notebook crypto backup failed") {
        // internal bug: inspect wrapped cause, add json:"-" tag to offending field
    }
}

Prevention

When it happens

Trigger: Calling saveNotebookCryptoBackup when the NotebookCrypto struct (or a field added to it) contains a value json.Marshal cannot encode — unsupported types (chan, func, cyclic references via custom marshalers) or a field's MarshalJSON returning an error.

Common situations: A recent code change added a non-serializable field to conf.NotebookCrypto; a custom MarshalJSON implementation (e.g. for KDFParams) hits an internal error; regression after struct refactoring.

Understand the failure class

Background: json.Marshal / "failed to marshal" errors in Go: why "unsupported type" happens and how to fix it — this error's family across 22 libraries.

Related errors


AI-assisted analysis of siyuan-note/siyuan@9f775e8a12 (2026-09-19). Data as JSON: /api/errors/f6817a8d749bbadf. Report an issue: GitHub.

Appendix: source

Thrown at kernel/model/crypto.go:404

	prepareBackupForWrite(&nc)
	nc.KEKMAC = computeKEKMAC(&nc, kek)
	if !notebookCryptoConfigurationComplete(&nc) {
		Conf.m.Unlock()
		return errors.New("cannot save incomplete notebook crypto configuration")
	}
	Conf.NotebookCrypto.Spec = nc.Spec
	Conf.NotebookCrypto.BackupID = nc.BackupID
	Conf.NotebookCrypto.CreatedAt = nc.CreatedAt
	Conf.NotebookCrypto.Checksum = nc.Checksum
	Conf.NotebookCrypto.KEKMAC = nc.KEKMAC // 保持 Conf 与备份文件的 KEKMAC 一致
	Conf.m.Unlock()
	backupPath := dataCryptoBackupPath()
	if err := os.MkdirAll(filepath.Dir(backupPath), 0755); err != nil {
		return fmt.Errorf("mkdir notebook crypto backup dir failed: %w", err)
	}
	data, err := json.Marshal(nc)
	if err != nil {
		return fmt.Errorf("marshal notebook crypto backup failed: %w", err)
	}
	if err := atomicWriteFile(backupPath, data); err != nil {
		return fmt.Errorf("write notebook crypto backup failed: %w", err)
	}
	return nil
}

// writeNotebookCryptoBackupData 将指定的 NotebookCrypto 写入备份文件(不依赖 Conf.NotebookCrypto)。
// kek 必须非 nil:在 Checksum 定型后计算 KEKMAC,保证落盘 MAC 与落盘内容一致。
func writeNotebookCryptoBackupData(nc *conf.NotebookCrypto, kek []byte) error {
	if kek == nil {
		return errors.New("cannot generate notebook crypto backup without KEK")
	}
	prepareBackupForWrite(nc)
	nc.KEKMAC = computeKEKMAC(nc, kek)
	if !notebookCryptoConfigurationComplete(nc) {
		return errors.New("cannot write incomplete notebook crypto backup")
	}

View on GitHub (pinned to 9f775e8a12)