{"record":{"id":"d1ca41e156bd5778","repo":"gofr-dev/gofr","slug":"w-creating-index-w","errorCode":null,"errorMessage":"%w: creating index: %w","messagePattern":"%w: creating index: %w","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"critical","filePath":"pkg/gofr/datasource/elasticsearch/elasticsearch.go","lineNumber":136,"sourceCode":"\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),\n\t\t[]string{index}, \"\", settings, span)\n\n\treturn nil\n}\n\nfunc (c *Client) DeleteIndex(ctx context.Context, index string) error {\n\tif strings.TrimSpace(index) == \"\" {\n\t\treturn errEmptyIndex\n\t}\n","sourceCodeStart":118,"sourceCodeEnd":154,"githubUrl":"https://github.com/gofr-dev/gofr/blob/187eb24962502e91f1fee856230670958b66e89c/pkg/gofr/datasource/elasticsearch/elasticsearch.go#L118-L154","documentation":"This is errOperation wrapped around the error from esapi.IndicesCreateRequest.Do in CreateIndex. It means the HTTP request to Elasticsearch never completed at the transport layer: connection refused, DNS failure, TLS error, timeout, or a cancelled context. No HTTP response was produced, unlike errResponse cases.","triggerScenarios":"Calling CreateIndex when the ES cluster is unreachable: wrong Addresses in Config, cluster down, network/firewall blocking the port, client not yet connected (Connect() failed and c.client is effectively unusable), or the ctx passed in is already cancelled/expired.","commonSituations":"Developers hit this in dev environments where ES runs in Docker but the app uses localhost incorrectly, after a cluster restart, with wrong port (9200 vs 9300), during TLS misconfiguration, or when a short request context expires before the call finishes.","solutions":["Verify Config.Addresses points at a reachable ES HTTP endpoint (default port 9200).","Check cluster/container health: curl the ES endpoint directly from the app host.","Confirm Connect() succeeded and the health check passed at startup.","Inspect the inner wrapped error for connection refused vs DNS vs timeout to target the fix.","Ensure the passed context has sufficient deadline and is not already cancelled."],"exampleFix":"// before\nerr := client.CreateIndex(ctx, \"orders\", settings) // ctx already cancelled\n// after\nctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)\ndefer cancel()\nerr := client.CreateIndex(ctx, \"orders\", settings)","handlingStrategy":"retry","validationCode":"// verify reachability before calling\nctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)\ndefer cancel()\nif _, err := client.HealthCheck(ctx); err != nil {\n    return fmt.Errorf(\"elasticsearch unreachable: %w\", err)\n}","typeGuard":null,"tryCatchPattern":"var lastErr error\nfor attempt := 0; attempt < 3; attempt++ {\n    ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)\n    err := client.CreateIndex(ctx, index, settings)\n    cancel()\n    if err == nil {\n        break\n    }\n    lastErr = err\n    if strings.Contains(err.Error(), \"elasticsearch operation error\") {\n        time.Sleep(time.Duration(attempt+1) * time.Second) // transport error: retry\n        continue\n    }\n    return err // non-transport error: do not retry\n}\nreturn lastErr","preventionTips":["Verify Addresses and port (9200) in config for each environment.","Call Connect()/HealthCheck at startup and fail fast with a clear message.","Always pass contexts with adequate deadlines for index operations.","Add readiness checks in docker-compose/k8s so the app starts after ES is up."],"tags":["elasticsearch","network","connection","timeout"],"backgroundTag":"connection-refused","analyzedSha":"187eb24962502e91f1fee856230670958b66e89c","analyzedAt":"2026-09-01T20:34:54.554Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-08T10:18:20.063Z"}