{"record":{"id":"46e3b8a51c3a1955","repo":"siyuan-note/siyuan","slug":"marshal-inline-styles-failed-w","errorCode":null,"errorMessage":"marshal inline styles failed: %w","messagePattern":"marshal inline styles failed: %w","errorType":"error_code","errorClass":null,"httpStatus":null,"severity":"error","filePath":"kernel/model/inline_style.go","lineNumber":279,"sourceCode":"\tif 0 < len(changedCustomColorIndexes) {\n\t\tremovedIndexes := removedAttributeViewCustomColorIndexes(currentAV.Colors, ret.AV.Colors)\n\t\tcustomColorUsage, err = workspaceAttributeViewCustomColorUsage(0 < len(removedIndexes))\n\t\tif err != nil {\n\t\t\treturn nil, false, err\n\t\t}\n\t\tfor _, avID := range sortedAttributeViewUsageIDs(customColorUsage) {\n\t\t\tindexes := customColorUsage[avID]\n\t\t\tfor index := range removedIndexes {\n\t\t\t\tif _, used := indexes[index]; used {\n\t\t\t\t\treturn nil, false, fmt.Errorf(\"attribute view custom color [%d] is still in use by attribute view [%s]\",\n\t\t\t\t\t\tindex, avID)\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t}\n\tdata, err := gulu.JSON.MarshalIndentJSON(ret, \"\", \"  \")\n\tif err != nil {\n\t\treturn nil, false, fmt.Errorf(\"marshal inline styles failed: %w\", err)\n\t}\n\tif maxInlineStylesFileSize < len(data) {\n\t\treturn nil, false, fmt.Errorf(\"inline styles file exceeds the %d byte limit\", maxInlineStylesFileSize)\n\t}\n\n\tdataPath := inlineStylesPath()\n\toldData, readErr := filelock.ReadFile(dataPath)\n\tif readErr != nil && !os.IsNotExist(readErr) {\n\t\treturn nil, false, fmt.Errorf(\"read inline styles failed: %w\", readErr)\n\t}\n\tif bytes.Equal(oldData, data) {\n\t\tcacheWorkspaceAVPalette(ret.AV)\n\t\treturn ret, false, nil\n\t}\n\n\tif err = os.MkdirAll(filepath.Dir(dataPath), 0755); err != nil {\n\t\treturn nil, false, fmt.Errorf(\"create inline styles directory failed: %w\", err)\n\t}","sourceCodeStart":261,"sourceCodeEnd":297,"githubUrl":"https://github.com/siyuan-note/siyuan/blob/8641553a1f07374001902d3ce773285db1292b2d/kernel/model/inline_style.go#L261-L297","documentation":"After merging the incoming styles with the current AV state, the result is serialized with gulu.JSON.MarshalIndentJSON before being written to disk. A marshal failure here means the resulting InlineStyles structure could not be converted to JSON — typically caused by unsupported types or a MarshalJSON implementation error deeper in the structure. The write is aborted and the original file untouched.","triggerScenarios":"A custom type inside InlineStyles/InlineStyleAV (or injected fields) whose MarshalJSON returns an error; corrupted in-memory structures containing channels/funcs that json cannot encode (usually only possible via code modifications or plugin-supplied values).","commonSituations":"Custom kernel builds adding non-serializable fields to InlineStyles; middleware that stores arbitrary values into the styles map before persisting.","solutions":["Inspect any custom MarshalJSON on InlineStyles/InlineStyle types and fix the error it returns","Remove or make JSON-serializable any non-standard fields added to the styles structures","Log the underlying wrapped error (%w) to identify the exact field that failed","Retry with a minimal payload (empty Styles) to confirm the failure comes from payload content"],"exampleFix":"// before\ntype InlineStyle struct { Callback func() `json:\"cb\"` } // json cannot marshal funcs\n// after\ntype InlineStyle struct { Color string `json:\"color\"` }","handlingStrategy":"try-catch","validationCode":"if _, err := json.Marshal(ret); err != nil {\n    return fmt.Errorf(\"payload not serializable: %w\", err)\n}","typeGuard":null,"tryCatchPattern":"_, _, err := model.SetInlineStyles(styles)\nif err != nil && strings.HasPrefix(err.Error(), \"marshal inline styles failed\") {\n    log.Fatalf(\"styles payload not serializable: %v\", err)\n}","preventionTips":["Keep only JSON-serializable fields in InlineStyles/InlineStyle structures","Test any custom MarshalJSON implementations against the full styles structure","Avoid injecting runtime-only values (funcs, channels) into palette data"],"tags":["go","json","serialization"],"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-14T00:17:10.932Z"}