{"record":{"id":"12a45ef65c43afe8","repo":"bcicen/ctop","slug":"failed-to-write-config-s","errorCode":null,"errorMessage":"failed to write config: %s","messagePattern":"failed to write config: (.+?)","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"config/file.go","lineNumber":109,"sourceCode":"\t\t}\n\t}\n\n\t// remove prior to writing new file\n\tif err := os.Remove(path); err != nil {\n\t\tif !os.IsNotExist(err) {\n\t\t\treturn path, err\n\t\t}\n\t}\n\n\tfile, err := os.OpenFile(path, os.O_RDWR|os.O_CREATE, 0644)\n\tif err != nil {\n\t\treturn path, fmt.Errorf(\"failed to open config for writing: %s\", err)\n\t}\n\n\twriter := toml.NewEncoder(file)\n\terr = writer.Encode(exportConfig())\n\tif err != nil {\n\t\treturn path, fmt.Errorf(\"failed to write config: %s\", err)\n\t}\n\n\treturn path, nil\n}\n\n// determine config path from environment\nfunc getConfigPath() (path string, err error) {\n\thomeDir, ok := os.LookupEnv(\"HOME\")\n\tif !ok {\n\t\treturn path, fmt.Errorf(\"$HOME not set\")\n\t}\n\n\t// use xdg config home if possible\n\tif xdgSupport() {\n\t\txdgHome, ok := os.LookupEnv(\"XDG_CONFIG_HOME\")\n\t\tif !ok {\n\t\t\txdgHome = fmt.Sprintf(\"%s/.config\", homeDir)\n\t\t}","sourceCodeStart":91,"sourceCodeEnd":127,"githubUrl":"https://github.com/bcicen/ctop/blob/59f00dd6aaebf48f3bc501e174f75f815c2619f0/config/file.go#L91-L127","documentation":"After the config file opens successfully, Write encodes exportConfig() as TOML via toml.NewEncoder(file).Encode. If encoding/writing to the file fails, the error is wrapped as 'failed to write config'.","triggerScenarios":"toml Encoder.Encode returns an error — typically an I/O write failure (disk full, closed handle) or a value in the config struct that the TOML encoder cannot serialize (e.g. unsupported type like a map with non-string keys, channel, func).","commonSituations":"Disk full on the volume holding $HOME; config struct gained a field of an unsupported type after an upgrade; file replaced by a pipe/fifo or special file mid-write.","solutions":["Check free disk space on the filesystem holding the config","Audit the exported config struct for TOML-unsupported types (channels, funcs, complex numbers, non-string map keys)","Re-run write after removing any special file at the config path"],"exampleFix":"// before\ntype Config struct { Callbacks map[int]func() } // unsupported by TOML\n// after\ntype Config struct { Callbacks map[string]string }","handlingStrategy":"try-catch","validationCode":"// keep config struct TOML-encodable: no funcs/channels/non-string map keys\ntoml.Marshal(exportConfig()) // dry-run encode to memory first","typeGuard":null,"tryCatchPattern":"_, err := config.Write()\nif err != nil && strings.HasPrefix(err.Error(), \"failed to write config\") {\n    // free disk space / fix unsupported field types\n}","preventionTips":["Monitor free disk space on the config volume","Dry-run TOML encode in tests to catch unsupported types","Avoid special files (fifo/socket) at the config path"],"tags":["toml","serialization","filesystem"],"backgroundTag":"config-serialization-failed","analyzedSha":"59f00dd6aaebf48f3bc501e174f75f815c2619f0","analyzedAt":"2026-09-02T23:34:03.832Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-10T02:17:09.455Z"}