alibaba/open-code-review · error

parse config: %w

Error message

parse config: %w

What it means

loadOrCreateConfig parses the config file with json.Unmarshal into Config and wraps any unmarshal failure as 'parse config: %w'. This means the file was read successfully but its content is not valid JSON, or its JSON does not match the Config schema (e.g. wrong types for fields).

Source

Thrown at cmd/opencodereview/config_cmd.go:396

// TelemetryConfig holds telemetry-specific settings.
type TelemetryConfig struct {
	Enabled      bool   `json:"enabled,omitempty"`         // Master switch for telemetry
	Exporter     string `json:"exporter,omitempty"`        // "console" or "otlp"
	OTLPEndpoint string `json:"otlp_endpoint,omitempty"`   // OTLP collector address
	ContentLog   bool   `json:"content_logging,omitempty"` // Include prompt/response content
}

func loadOrCreateConfig(path string) (*Config, error) {
	data, err := os.ReadFile(path)
	if err != nil {
		if os.IsNotExist(err) {
			return &Config{}, nil
		}
		return nil, err
	}
	var cfg Config
	if err := json.Unmarshal(data, &cfg); err != nil {
		return nil, fmt.Errorf("parse config: %w", err)
	}
	return &cfg, nil
}

// LoadAppConfig loads config from path. Returns nil, nil if file does not exist.
func LoadAppConfig(path string) (*Config, error) {
	data, err := os.ReadFile(path)
	if err != nil {
		if os.IsNotExist(err) {
			return nil, nil
		}
		return nil, fmt.Errorf("read app config %s: %w", path, err)
	}
	var cfg Config
	if err := json.Unmarshal(data, &cfg); err != nil {
		return nil, fmt.Errorf("parse app config: %w", err)
	}
	return &cfg, nil

View on GitHub (pinned to 5cf97d0d15)

Solutions

  1. Read the wrapped %w message for the exact JSON offset/reason.
  2. Validate the file with a JSON linter (jq . config.json) and fix the syntax or type.
  3. Restore the config from backup or delete it so loadOrCreateConfig recreates an empty one.
  4. Avoid hand-editing; use 'ocr config set' commands to modify values.

Example fix

// before
{ "maxTokens": "4096", }

// after
{ "maxTokens": 4096 }
Defensive patterns

Strategy: validation

Validate before calling

data, err := os.ReadFile(configPath)
if err != nil { return err }
var probe map[string]any
if err := json.Unmarshal(data, &probe); err != nil {
    return fmt.Errorf("fix config JSON before running ocr config: %w", err)
}

Type guard

func isConfigJSON(data []byte) bool {
    var cfg Config
    return json.Unmarshal(data, &cfg) == nil
}

Try / catch

if err := run(); err != nil {
    var uerr *json.UnmarshalTypeError
    if errors.As(err, &uerr) {
        fmt.Fprintf(os.Stderr, "config field %s has wrong type at offset %d\n", uerr.Field, uerr.Offset)
    }
}

Prevention

When it happens

Trigger: Any config-mutating command ('ocr config set/unset ...') when the existing config file contains malformed JSON or type-incompatible values (e.g. maxTokens as a string instead of number).

Common situations: Hand-edited config with a missing comma or trailing comma; comments pasted into JSON; a tool wrote YAML/JSON5 to a .json file; number field changed to string in an editor.

Related errors


AI-assisted analysis of alibaba/open-code-review@5cf97d0d15 (2026-09-02). Data as JSON: /api/errors/580d5e6b4b53fce2. Report an issue: GitHub.