{"record":{"id":"7c0a793139154977","repo":"gofr-dev/gofr","slug":"w-settings-w","errorCode":null,"errorMessage":"%w: settings: %w","messagePattern":"%w: settings: %w","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"pkg/gofr/datasource/elasticsearch/elasticsearch.go","lineNumber":126,"sourceCode":"\t\treturn\n\t}\n\n\tc.logger.Logf(\"connected to Elasticsearch successfully at : %v\", c.config.Addresses)\n}\n\n// CreateIndex creates an index in Elasticsearch with the specified settings.\nfunc (c *Client) CreateIndex(ctx context.Context, index string, settings map[string]any) error {\n\tif strings.TrimSpace(index) == \"\" {\n\t\treturn errEmptyIndex\n\t}\n\n\tstart := time.Now()\n\n\ttracedCtx, span := c.addTrace(ctx, \"create-index\", []string{index}, \"\")\n\n\tbody, err := json.Marshal(settings)\n\tif err != nil {\n\t\treturn fmt.Errorf(\"%w: settings: %w\", errMarshaling, err)\n\t}\n\n\treq := esapi.IndicesCreateRequest{\n\t\tIndex: index,\n\t\tBody:  bytes.NewReader(body),\n\t}\n\n\tres, err := req.Do(tracedCtx, c.client)\n\tif err != nil {\n\t\treturn fmt.Errorf(\"%w: creating index: %w\", errOperation, err)\n\t}\n\tdefer res.Body.Close()\n\n\tif res.IsError() {\n\t\treturn fmt.Errorf(\"%w: %s\", errResponse, res.String())\n\t}\n\n\tc.sendOperationStats(start, fmt.Sprintf(\"CREATE INDEX %s\", index),","sourceCodeStart":108,"sourceCodeEnd":144,"githubUrl":"https://github.com/gofr-dev/gofr/blob/187eb24962502e91f1fee856230670958b66e89c/pkg/gofr/datasource/elasticsearch/elasticsearch.go#L108-L144","documentation":"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.","triggerScenarios":"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.","commonSituations":"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.","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."],"exampleFix":"// before\nsettings := map[string]any{\"number_of_shards\": 1, \"callback\": onUpdate}\nerr := client.CreateIndex(ctx, \"orders\", settings) // error marshaling data: settings: ...\n// after\nsettings := map[string]any{\"number_of_shards\": 1}\nerr := client.CreateIndex(ctx, \"orders\", settings)","handlingStrategy":"validation","validationCode":"if _, err := json.Marshal(settings); err != nil {\n    return fmt.Errorf(\"invalid index settings: %w\", err)\n}\nerr := client.CreateIndex(ctx, index, settings)","typeGuard":null,"tryCatchPattern":"if err := client.CreateIndex(ctx, index, settings); err != nil {\n    var msg string\n    if errors.As(err, &target) || strings.Contains(err.Error(), \"error marshaling data\") {\n        msg = \"settings map is not JSON-serializable\"\n    }\n    return fmt.Errorf(\"create index failed: %v: %w\", msg, err)\n}","preventionTips":["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."],"tags":["elasticsearch","json-marshal","create-index"],"backgroundTag":"json-marshal-failed","analyzedSha":"187eb24962502e91f1fee856230670958b66e89c","analyzedAt":"2026-09-01T20:34:54.554Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-08T10:18:20.063Z"}