{"record":{"id":"cfe8a6b99ade7c99","repo":"yorukot/superfile","slug":"error-encoding-data-w","errorCode":null,"errorMessage":"error encoding data : %w","messagePattern":"error encoding data : %w","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"src/pkg/utils/file_utils.go","lineNumber":29,"sourceCode":"\t\"reflect\"\n\t\"strings\"\n\n\t\"github.com/adrg/xdg\"\n\t\"github.com/pelletier/go-toml/v2\"\n\n\t\"github.com/charmbracelet/x/ansi\"\n\t\"golang.org/x/text/encoding/unicode\"\n\t\"golang.org/x/text/transform\"\n)\n\n// Utility functions related to file operations\n// Note : This is not used anymore as we use os.WriteAt to\n// fix toml files now, but we will still keep it for later use.\nfunc WriteTomlData(filePath string, data interface{}) error {\n\ttomlData, err := toml.Marshal(data)\n\tif err != nil {\n\t\t// return a wrapped error\n\t\treturn fmt.Errorf(\"error encoding data : %w\", err)\n\t}\n\terr = os.WriteFile(filePath, tomlData, ConfigFilePerm)\n\tif err != nil {\n\t\treturn fmt.Errorf(\"error writing file : %w\", err)\n\t}\n\treturn nil\n}\n\n// Helper function to load and validate TOML files with field checking\n// errorPrefix is appended before every error message\nfunc LoadTomlFile(filePath string, defaultData string, target interface{},\n\tfixFlag bool, ignoreMissingFields bool) error {\n\t// Initialize with default config\n\t_ = toml.Unmarshal([]byte(defaultData), target)\n\n\tdata, err := os.ReadFile(filePath)\n\tif err != nil {\n\t\treturn &TomlLoadError{","sourceCodeStart":11,"sourceCodeEnd":47,"githubUrl":"https://github.com/yorukot/superfile/blob/b72f550bc6e75913f48eb49fdd258e1a2df2fe88/src/pkg/utils/file_utils.go#L11-L47","documentation":"WriteTomlData marshals an arbitrary Go value to TOML before writing it to disk; this error wraps a failure from toml.Marshal. Marshal fails when the in-memory value cannot be represented as TOML. The file is untouched in this case since the failure happens before writing.","triggerScenarios":"Calling WriteTomlData(filePath, data) where data contains types the TOML encoder cannot handle — e.g. nil interface values, maps with non-string keys, nested unsupported types (pointers to unsupported kinds, channels, funcs), or unexported/invalid struct shapes.","commonSituations":"Config structs refactored to include unsupported fields (e.g. map[int]X or time-incompatible types); passing a raw nil; third-party types with no TOML representation embedded in the config; version changes in the TOML library tightening type rules.","solutions":["Log the unwrapped marshal error (errors.Unwrap) to identify the offending field/type.","Change the config struct so all exported fields are TOML-marshalable (string-keyed maps, basic types, slices, nested structs).","Add custom marshaling (MarshalTOML / TextMarshaler) for types that need representation.","Note this function is deprecated in favor of os.WriteAt-based saving — migrate to the current save path if maintainable."],"exampleFix":"// before\ntype Config struct { Ports map[int]string }\nerr := utils.WriteTomlData(path, cfg) // map[int]string not TOML-encodable\n// after\ntype Config struct { Ports map[string]string }\nerr := utils.WriteTomlData(path, cfg)","handlingStrategy":"try-catch","validationCode":"func isTOMLMarshalable(v reflect.Value) bool {\n    switch v.Kind() {\n    case reflect.Map:\n        return v.Type().Key().Kind() == reflect.String\n    case reflect.Struct, reflect.Slice, reflect.Array, reflect.Pointer:\n        return true\n    default:\n        return true\n    }\n}\n// reject map[int]... etc. before calling WriteTomlData","typeGuard":null,"tryCatchPattern":"if err := utils.WriteTomlData(path, cfg); err != nil {\n    if strings.Contains(err.Error(), \"error encoding data\") {\n        return fmt.Errorf(\"config value not TOML-encodable: %w\", err)\n    }\n    return err\n}","preventionTips":["Keep config structs limited to TOML-supported types (string-keyed maps, basics, slices, nested structs).","Add marshal round-trip tests for the config struct.","Avoid embedding third-party types without TOML representations.","Distinguish encode errors from write errors when logging."],"tags":["toml","encoding","config"],"backgroundTag":"toml-marshal-failed","analyzedSha":"b72f550bc6e75913f48eb49fdd258e1a2df2fe88","analyzedAt":"2026-09-01T04:38:08.254Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-08T10:18:20.063Z"}