Tencent/WeKnora · error

%w: config is nil

Error message

%w: config is nil

What it means

parseIMAConfig in the IMA datasource connector rejects a nil *types.DataSourceConfig by wrapping datasource.ErrInvalidConfig. It is a fast-fail sentinel so callers (Validate, ListResources, walk) get a consistent, errors.Is-matchable error instead of a nil-pointer panic during the JSON marshal/unmarshal roundtrip of credentials.

Source

Thrown at internal/datasource/connector/ima/types.go:59

}

// GetBaseURL returns the normalized base URL (empty → default, no trailing slash).
func (c *Config) GetBaseURL() string {
	url := strings.TrimSpace(c.BaseURL)
	if url == "" {
		return DefaultBaseURL
	}
	if !strings.Contains(url, "://") {
		url = "https://" + url
	}
	return strings.TrimRight(url, "/")
}

// parseIMAConfig extracts and validates IMA-specific configuration.
// Uses JSON marshal/unmarshal roundtrip so extra fields are ignored gracefully.
func parseIMAConfig(config *types.DataSourceConfig) (*Config, error) {
	if config == nil {
		return nil, fmt.Errorf("%w: config is nil", datasource.ErrInvalidConfig)
	}
	credBytes, err := json.Marshal(config.Credentials)
	if err != nil {
		return nil, fmt.Errorf("marshal credentials: %w", err)
	}
	var cfg Config
	if err := json.Unmarshal(credBytes, &cfg); err != nil {
		return nil, fmt.Errorf("parse ima credentials: %w", err)
	}
	if strings.TrimSpace(cfg.ClientID) == "" {
		return nil, fmt.Errorf("%w: client_id is required", datasource.ErrInvalidCredentials)
	}
	if strings.TrimSpace(cfg.APIKey) == "" {
		return nil, fmt.Errorf("%w: api_key is required", datasource.ErrInvalidCredentials)
	}
	if err := datasource.ValidateConnectorBaseURL(cfg.GetBaseURL()); err != nil {
		return nil, err
	}

View on GitHub (pinned to 988cbb0330)

Solutions

  1. Ensure the IMA DataSourceConfig is constructed and populated before calling connector methods
  2. Skip or error on nil entries when iterating a slice of DataSourceConfig before invoking connectors
  3. Use errors.Is(err, datasource.ErrInvalidConfig) to detect this case and return a clear 'datasource not configured' message to users

Example fix

// before
for _, cfg := range configs { // configs may contain nil
    if err := connector.Validate(ctx, cfg); err != nil { ... }
}
// after
for _, cfg := range configs {
    if cfg == nil {
        return fmt.Errorf("datasource config is nil");
    }
    if err := connector.Validate(ctx, cfg); err != nil { ... }
}
Defensive patterns

Strategy: type-guard

Validate before calling

if config == nil {
    return fmt.Errorf("ima datasource not configured: DataSourceConfig is nil")
}
if len(config.Credentials) == 0 {
    return fmt.Errorf("ima datasource credentials are empty")
}

Type guard

func hasIMAConfig(cfg *types.DataSourceConfig) bool { return cfg != nil && cfg.Credentials != nil }

Try / catch

cfg, err := parseIMAConfig(config)
if err != nil {
    if errors.Is(err, datasource.ErrInvalidConfig) {
        return fmt.Errorf("IMA datasource is not configured (nil DataSourceConfig); add it to your datasource config: %w", err)
    }
    return err
}

Prevention

When it happens

Trigger: Calling Validate, ListResources, or walk on an IMA connector with a nil DataSourceConfig pointer; tests (TestParseIMAConfig_NilConfig) also trigger it intentionally. Practically this means the datasource was not loaded/registered before use, or a nil entry slipped into a collection of datasource configs.

Common situations: Config file missing the IMA datasource entry but code iterating all datasources regardless; a slice of configs containing a nil element; refactors returning nil, nil instead of an error from config loaders.

Related errors


AI-assisted analysis of Tencent/WeKnora@988cbb0330 (2026-09-02). Data as JSON: /api/errors/b27aa8f65aed6214. Report an issue: GitHub.