{"record":{"id":"1910457ef78e4d19","repo":"gofr-dev/gofr","slug":"w-executing-bulk-w","errorCode":null,"errorMessage":"%w: executing bulk: %w","messagePattern":"%w: executing bulk: %w","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"critical","filePath":"pkg/gofr/datasource/elasticsearch/elasticsearch.go","lineNumber":250,"sourceCode":"\n\tstart := time.Now()\n\ttracedCtx, span := c.addTrace(ctx, \"bulk\", nil, \"\")\n\n\tvar buf bytes.Buffer\n\tfor _, op := range operations {\n\t\tif err := json.NewEncoder(&buf).Encode(op); err != nil {\n\t\t\treturn nil, fmt.Errorf(\"%w: %w\", errEncodingOperation, err)\n\t\t}\n\t}\n\n\treq := esapi.BulkRequest{\n\t\tBody:    &buf,\n\t\tRefresh: \"true\",\n\t}\n\n\tres, err := req.Do(tracedCtx, c.client)\n\tif err != nil {\n\t\treturn nil, fmt.Errorf(\"%w: executing bulk: %w\", errOperation, err)\n\t}\n\n\tdefer res.Body.Close()\n\n\tif res.IsError() {\n\t\treturn nil, fmt.Errorf(\"%w: %s\", errResponse, res.String())\n\t}\n\n\tvar result map[string]any\n\tif err := json.NewDecoder(res.Body).Decode(&result); err != nil {\n\t\treturn nil, fmt.Errorf(\"%w: %w\", errParsingResponse, err)\n\t}\n\n\tc.sendOperationStats(start, \"BULK\", nil, \"\", operations, span)\n\n\treturn result, nil\n}\n","sourceCodeStart":232,"sourceCodeEnd":268,"githubUrl":"https://github.com/gofr-dev/gofr/blob/187eb24962502e91f1fee856230670958b66e89c/pkg/gofr/datasource/elasticsearch/elasticsearch.go#L232-L268","documentation":"Bulk() sends the prepared esapi.BulkRequest via req.Do(tracedCtx, c.client). If the transport-level request fails — network failure, connection refused, timeout, TLS error, or client not initialized — the method returns fmt.Errorf(\"%w: executing bulk: %w\", errOperation, err), wrapping the errOperation sentinel plus a contextual \"executing bulk\" message and the underlying error. No response was received, so this is always a request-execution problem, not a server-side bulk result.","triggerScenarios":"Calling Bulk() while the Elasticsearch node is unreachable (down, wrong host/port), DNS failure, connection timeouts under load, context cancellation before Do completes, or c.client being nil/misconfigured.","commonSituations":"ES cluster restart or rolling upgrade during a batch job; wrong ELASTICSEARCH_HOSTS env var; container networking issues in Kubernetes; firewall dropping port 9200; bulk job running past a context deadline.","solutions":["Unwrap and log the underlying error (errors.Unwrap) to distinguish connection refused vs timeout vs context canceled.","Verify the Elasticsearch endpoint host/port and that the node is up: curl http://host:9200/_cluster/health.","Implement retry with backoff for transient network errors, and use a context with adequate timeout.","Check DNS/container networking and firewall rules if connection refused persists.","Confirm the client was Connected()/initialized before calling Bulk — a nil client produces transport errors."],"exampleFix":"// before\nres, err := client.Bulk(ctx, ops)\nif err != nil { return err }\n// after\nctx, cancel := context.WithTimeout(context.Background(), 30*time.Second)\ndefer cancel()\nres, err := client.Bulk(ctx, ops)\nif err != nil {\n    if isRetryable(err) { // net.Error timeout, connection refused\n        return retryWithBackoff(ops)\n    }\n    return err\n}","handlingStrategy":"retry","validationCode":"// before calling Bulk, confirm reachability\nresp, err := http.Get(esURL + \"/_cluster/health\")\nif err != nil || resp.StatusCode != 200 {\n    return fmt.Errorf(\"elasticsearch unreachable: %v\", err)\n}","typeGuard":null,"tryCatchPattern":"res, err := client.Bulk(ctx, ops)\nif err != nil {\n    var netErr net.Error\n    if errors.As(err, &netErr) || strings.Contains(err.Error(), \"connection refused\") {\n        // retry with exponential backoff; transient network issue\n        return backoffRetry(ops, 3)\n    }\n    return err\n}","preventionTips":["Always pass a context with a sane timeout to Bulk.","Health-check the ES endpoint before large batch jobs.","Verify client.Connect() succeeded at startup before issuing bulk operations.","Use connection pooling / keep-alives and monitor ES node availability."],"tags":["elasticsearch","network","bulk-api","connection"],"backgroundTag":"connection-refused","analyzedSha":"187eb24962502e91f1fee856230670958b66e89c","analyzedAt":"2026-09-01T20:34:54.554Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-08T10:18:20.063Z"}