gofr-dev/gofr · error
%w: settings: %w
Error message
%w: settings: %w
What it means
This is errMarshaling wrapped around a json.Marshal failure of the settings map in CreateIndex. Go's encoding/json cannot serialize every Go value; when settings contains channels, funcs, complex numbers, or a MarshalJSON implementation that errors, CreateIndex fails before any request is sent to the cluster.
Source
Thrown at pkg/gofr/datasource/elasticsearch/elasticsearch.go:126
return
}
c.logger.Logf("connected to Elasticsearch successfully at : %v", c.config.Addresses)
}
// CreateIndex creates an index in Elasticsearch with the specified settings.
func (c *Client) CreateIndex(ctx context.Context, index string, settings map[string]any) error {
if strings.TrimSpace(index) == "" {
return errEmptyIndex
}
start := time.Now()
tracedCtx, span := c.addTrace(ctx, "create-index", []string{index}, "")
body, err := json.Marshal(settings)
if err != nil {
return fmt.Errorf("%w: settings: %w", errMarshaling, err)
}
req := esapi.IndicesCreateRequest{
Index: index,
Body: bytes.NewReader(body),
}
res, err := req.Do(tracedCtx, c.client)
if err != nil {
return fmt.Errorf("%w: creating index: %w", errOperation, err)
}
defer res.Body.Close()
if res.IsError() {
return fmt.Errorf("%w: %s", errResponse, res.String())
}
c.sendOperationStats(start, fmt.Sprintf("CREATE INDEX %s", index),View on GitHub (pinned to 187eb24962)
Solutions
- Validate the settings map contains only JSON-safe values before calling CreateIndex.
- Test json.Marshal(settings) locally to reproduce the exact encoder error message.
- Replace non-serializable values with JSON primitives (string, number, bool, nil, slice, map).
- Fix any custom MarshalJSON implementations on values nested inside settings.
Example fix
// before
settings := map[string]any{"number_of_shards": 1, "callback": onUpdate}
err := client.CreateIndex(ctx, "orders", settings) // error marshaling data: settings: ...
// after
settings := map[string]any{"number_of_shards": 1}
err := client.CreateIndex(ctx, "orders", settings) Defensive patterns
Strategy: validation
Validate before calling
if _, err := json.Marshal(settings); err != nil {
return fmt.Errorf("invalid index settings: %w", err)
}
err := client.CreateIndex(ctx, index, settings) Try / catch
if err := client.CreateIndex(ctx, index, settings); err != nil {
var msg string
if errors.As(err, &target) || strings.Contains(err.Error(), "error marshaling data") {
msg = "settings map is not JSON-serializable"
}
return fmt.Errorf("create index failed: %v: %w", msg, err)
} Prevention
- Keep settings as static map/struct literals where possible; avoid dynamic any fields.
- Write a test that json.Marshal's every settings payload used in migrations.
- Prefer typed settings structs over map[string]any to catch issues at compile time.
When it happens
Trigger: Calling CreateIndex(ctx, index, settings) where the settings map contains a non-JSON-encodable value (func, chan, complex) or a custom json.Marshaler that returns an error.
Common situations: Developers hit this when building index settings programmatically (analyzers, mappings) and accidentally including non-serializable values, or when reusing structs with custom marshaling logic that has bugs.
Understand the failure class
Background: json.Marshal / "failed to marshal" errors in Go: why "unsupported type" happens and how to fix it — this error's family across 22 libraries.
Related errors
AI-assisted analysis of gofr-dev/gofr@187eb24962 (2026-09-01).
Data as JSON: /api/errors/7c0a793139154977.
Report an issue: GitHub.