{"record":{"id":"722e1c2d42860493","repo":"chenhg5/cc-connect","slug":"invalid-toml-w","errorCode":null,"errorMessage":"invalid TOML: %w","messagePattern":"invalid TOML: %w","errorType":"validation","errorClass":null,"httpStatus":null,"severity":"error","filePath":"config/config.go","lineNumber":3708,"sourceCode":"\t\treturn err\n\t}\n\tif err := tmp.Close(); err != nil {\n\t\tos.Remove(tmpPath)\n\t\treturn err\n\t}\n\treturn os.Rename(tmpPath, ConfigPath)\n}\n\n// FormatConfigFile reads the config file at the given path, formats it, and\n// writes it back. It validates the TOML syntax before writing.\nfunc FormatConfigFile(path string) error {\n\tdata, err := os.ReadFile(path)\n\tif err != nil {\n\t\treturn fmt.Errorf(\"read config: %w\", err)\n\t}\n\tcfg := &Config{}\n\tif err := toml.Unmarshal(data, cfg); err != nil {\n\t\treturn fmt.Errorf(\"invalid TOML: %w\", err)\n\t}\n\tformatted := formatTOML(string(data))\n\tif formatted == string(data) {\n\t\treturn nil\n\t}\n\tdir := filepath.Dir(path)\n\ttmp, err := os.CreateTemp(dir, \".config-*.tmp\")\n\tif err != nil {\n\t\treturn fmt.Errorf(\"create temp file: %w\", err)\n\t}\n\ttmpPath := tmp.Name()\n\tif _, err := tmp.WriteString(formatted); err != nil {\n\t\ttmp.Close()\n\t\tos.Remove(tmpPath)\n\t\treturn fmt.Errorf(\"write formatted config: %w\", err)\n\t}\n\tif err := tmp.Sync(); err != nil {\n\t\ttmp.Close()","sourceCodeStart":3690,"sourceCodeEnd":3726,"githubUrl":"https://github.com/chenhg5/cc-connect/blob/4000b2338aa6e850c99df54f8b0ed6ed7460b401/config/config.go#L3690-L3726","documentation":"FormatConfigFile first validates the TOML by unmarshalling it into Config before formatting; invalid syntax is wrapped as \"invalid TOML: %w\" so the formatter never rewrites a file it could not fully parse (which would risk destroying content). The wrapped decoder error pinpoints the line/column of the syntax problem.","triggerScenarios":"toml.Unmarshal fails in FormatConfigFile (config/config.go:3708) on malformed TOML: unterminated string, stray bracket, duplicate key, or a value whose type conflicts with the Config schema.","commonSituations":"Formatting a hand-edited config with a typo; trying to format a YAML/JSON file by mistake; truncated download or copy-paste cut the file short.","solutions":["Fix the TOML syntax at the line/column named in the wrapped decoder error.","Use a TOML linter/editor plugin to validate before re-running format.","Restore the file from backup if it was truncated; note format refuses to write until the file parses."],"exampleFix":"// before (config.toml)\nname = main\n// after\nname = \"main\"","handlingStrategy":"validation","validationCode":"data, err := os.ReadFile(path)\nif err != nil { return err }\nprobe := &config.Config{}\nif err := toml.Unmarshal(data, probe); err != nil {\n    return fmt.Errorf(\"%s is not valid TOML, formatter will refuse: %w\", path, err)\n}","typeGuard":null,"tryCatchPattern":"if err := config.FormatConfigFile(path); err != nil {\n    if strings.Contains(err.Error(), \"invalid TOML:\") {\n        slog.Error(\"fix TOML before formatting\", \"file\", path, \"detail\", err)\n        return err // do NOT overwrite the file\n    }\n    return err\n}","preventionTips":["Run a TOML validator/linter in your editor and CI on config.toml.","Never format as a 'fix' for a broken file — format validates first by design.","Keep backups/version history of hand-edited configs."],"tags":["config","toml","parse","validation"],"backgroundTag":"yaml-parse-error","analyzedSha":"4000b2338aa6e850c99df54f8b0ed6ed7460b401","analyzedAt":"2026-09-06T11:45:09.575Z","contentChangedAt":"2026-09-06T11:45:09.575Z","schemaVersion":2},"datasetVersion":"2026-09-14T00:17:10.932Z"}