chenhg5/cc-connect · error

encode config: %w

Error message

encode config: %w

What it means

saveConfig encodes the in-memory Config struct to TOML in memory before writing; if toml.NewEncoder(&buf).Encode(cfg) fails, it cleans up the temp file and returns this wrapped error. This indicates the Config value cannot be represented as TOML — rare, since the struct is designed for TOML round-tripping.

Source

Thrown at config/config.go:1557

	if err := toml.Unmarshal(data, cfg); err != nil {
		return nil, fmt.Errorf("parse config: %w", err)
	}
	return cfg, nil
}

func saveConfig(cfg *Config) error {
	dir := filepath.Dir(ConfigPath)
	tmp, err := os.CreateTemp(dir, ".config-*.tmp")
	if err != nil {
		return fmt.Errorf("create temp config: %w", err)
	}
	tmpPath := tmp.Name()

	var buf strings.Builder
	if err := toml.NewEncoder(&buf).Encode(cfg); err != nil {
		tmp.Close()
		os.Remove(tmpPath)
		return fmt.Errorf("encode config: %w", err)
	}

	formatted := formatTOML(buf.String())
	if _, err := tmp.WriteString(formatted); err != nil {
		tmp.Close()
		os.Remove(tmpPath)
		return fmt.Errorf("write config: %w", err)
	}
	if err := tmp.Sync(); err != nil {
		tmp.Close()
		os.Remove(tmpPath)
		return err
	}
	if err := tmp.Close(); err != nil {
		os.Remove(tmpPath)
		return err
	}
	return os.Rename(tmpPath, ConfigPath)

View on GitHub (pinned to 4000b2338a)

Solutions

  1. Inspect the wrapped encoder error for the offending field and change its value/type to a TOML-representable one (e.g. use map[string]any with string keys).
  2. Load the config via the library's Load path (which validates types) before mutating, instead of constructing Config from scratch.
  3. If a recent code change introduced a new Config field, ensure it has a TOML-compatible type and proper struct tags.

Example fix

// before
cfg.Extra = map[int]string{1: "x"} // non-string keys
// after
cfg.Extra = map[string]any{"1": "x"}
Defensive patterns

Strategy: try-catch

Try / catch

if err := saveConfigOp(); err != nil {
    if strings.Contains(err.Error(), "encode config") {
        // reload a pristine config from disk to drop the offending value
        cfg, lerr := config.Load()
        if lerr == nil { return reapplySafe(cfg) }
    }
    return err
}

Prevention

When it happens

Trigger: A Config struct containing a field type the TOML encoder cannot serialize (e.g. map with non-string keys, unsupported nested types) — typically only possible if Config was constructed programmatically with unusual values rather than loaded from a file.

Common situations: Programmatic config construction with non-string map keys or custom types assigned into map[string]any options; a schema change introducing a type the encoder rejects; memory/corruption edge cases.

Understand the failure class

Background: "JSON serialization failed", "not JSON serializable", "Failed to serialize": why JSON marshaling errors happen and how to fix them — this error's family across 46 libraries.

Related errors


AI-assisted analysis of chenhg5/cc-connect@4000b2338a (2026-09-06). Data as JSON: /api/errors/0a30a0f786ae7f0e. Report an issue: GitHub.