{"record":{"id":"81328df95765d796","repo":"gofr-dev/gofr","slug":"w-s-81328d","errorCode":null,"errorMessage":"%w: %s","messagePattern":"%w: %s","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"pkg/gofr/datasource/elasticsearch/elasticsearch.go","lineNumber":141,"sourceCode":"\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\n\tstart := time.Now()\n\n\ttracedCtx, span := c.addTrace(ctx, \"delete-index\", []string{index}, \"\")\n\n\treq := esapi.IndicesDeleteRequest{","sourceCodeStart":123,"sourceCodeEnd":159,"githubUrl":"https://github.com/gofr-dev/gofr/blob/187eb24962502e91f1fee856230670958b66e89c/pkg/gofr/datasource/elasticsearch/elasticsearch.go#L123-L159","documentation":"Same sentinel errResponse as index 10, raised in CreateIndex: the cluster responded with an HTTP error status for the create-index request. The raw ES response (including its error body) is appended via res.String(), so the precise exception type is in the message.","triggerScenarios":"CreateIndex(ctx, index, settings) where ES rejects the request: index already exists (resource_already_exists_exception), invalid index name (invalid_index_name_exception, e.g. uppercase or illegal characters), invalid settings payload rejected by the cluster (e.g. unknown analyzer or bad number_of_shards value), or 401/403 from insufficient permissions.","commonSituations":"Developers hit this when running migrations twice, using uppercase or wildcard-invalid index names, referencing a custom analyzer not defined in settings, or deploying with credentials lacking index-admin rights.","solutions":["Parse the wrapped res.String() message for the ES exception type.","For resource_already_exists_exception, check existence first or treat duplicate creation as a no-op.","For invalid_index_name_exception, use lowercase names without leading underscores, spaces, or forbidden characters.","For settings rejection, align settings with the cluster's ES version (e.g. type removal in ES 7+/8+).","For 401/403, fix credentials or grant index management permissions."],"exampleFix":"// before\nerr := client.CreateIndex(ctx, \"Orders\", settings) // invalid index name\n// after\nerr := client.CreateIndex(ctx, strings.ToLower(indexName), settings)","handlingStrategy":"type-guard","validationCode":"// validate index name before the call\nvar indexNameRe = regexp.MustCompile(`^[a-z0-9][a-z0-9_.-]*$`)\nif !indexNameRe.MatchString(index) {\n    return fmt.Errorf(\"invalid index name: %q\", index)\n}","typeGuard":"func IsIndexAlreadyExists(err error) bool {\n    return strings.Contains(err.Error(), \"resource_already_exists_exception\")\n}","tryCatchPattern":"if err := client.CreateIndex(ctx, index, settings); err != nil {\n    if IsIndexAlreadyExists(err) {\n        return nil // idempotent create\n    }\n    return fmt.Errorf(\"create index %s failed: %w\", index, err)\n}","preventionTips":["Treat CreateIndex as idempotent; check existence or swallow resource_already_exists_exception.","Enforce lowercase, valid index names with a shared validation helper.","Keep settings compatible with the deployed ES version; test migrations against the same version.","Grant service credentials only the permissions they need, including index management where required."],"tags":["elasticsearch","create-index","http-response"],"backgroundTag":"elasticsearch-error-response","analyzedSha":"187eb24962502e91f1fee856230670958b66e89c","analyzedAt":"2026-09-01T20:34:54.554Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-08T10:18:20.063Z"}