yorukot/superfile · error
error encoding data : %w
Error message
error encoding data : %w
What it means
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.
Source
Thrown at src/pkg/utils/file_utils.go:29
"reflect"
"strings"
"github.com/adrg/xdg"
"github.com/pelletier/go-toml/v2"
"github.com/charmbracelet/x/ansi"
"golang.org/x/text/encoding/unicode"
"golang.org/x/text/transform"
)
// Utility functions related to file operations
// Note : This is not used anymore as we use os.WriteAt to
// fix toml files now, but we will still keep it for later use.
func WriteTomlData(filePath string, data interface{}) error {
tomlData, err := toml.Marshal(data)
if err != nil {
// return a wrapped error
return fmt.Errorf("error encoding data : %w", err)
}
err = os.WriteFile(filePath, tomlData, ConfigFilePerm)
if err != nil {
return fmt.Errorf("error writing file : %w", err)
}
return nil
}
// Helper function to load and validate TOML files with field checking
// errorPrefix is appended before every error message
func LoadTomlFile(filePath string, defaultData string, target interface{},
fixFlag bool, ignoreMissingFields bool) error {
// Initialize with default config
_ = toml.Unmarshal([]byte(defaultData), target)
data, err := os.ReadFile(filePath)
if err != nil {
return &TomlLoadError{View on GitHub (pinned to b72f550bc6)
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.
Example fix
// before
type Config struct { Ports map[int]string }
err := utils.WriteTomlData(path, cfg) // map[int]string not TOML-encodable
// after
type Config struct { Ports map[string]string }
err := utils.WriteTomlData(path, cfg) Defensive patterns
Strategy: try-catch
Validate before calling
func isTOMLMarshalable(v reflect.Value) bool {
switch v.Kind() {
case reflect.Map:
return v.Type().Key().Kind() == reflect.String
case reflect.Struct, reflect.Slice, reflect.Array, reflect.Pointer:
return true
default:
return true
}
}
// reject map[int]... etc. before calling WriteTomlData Try / catch
if err := utils.WriteTomlData(path, cfg); err != nil {
if strings.Contains(err.Error(), "error encoding data") {
return fmt.Errorf("config value not TOML-encodable: %w", err)
}
return err
} Prevention
- 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.
When it happens
Trigger: 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.
Common situations: 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.
Related errors
- could not unmarshal user's theme file(%s) : %w
- could not read user's theme file(%s), err : %w
- failed to encode kitty graphics: %w
- error writing file : %w
- failed to create directory %s: %w
AI-assisted analysis of yorukot/superfile@b72f550bc6 (2026-09-01).
Data as JSON: /api/errors/cfe8a6b99ade7c99.
Report an issue: GitHub.