{"record":{"id":"585dc6f1d336f206","repo":"gofr-dev/gofr","slug":"elasticsearch-operation-error","errorCode":null,"errorMessage":"elasticsearch operation error","messagePattern":"elasticsearch operation error","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"pkg/gofr/datasource/elasticsearch/elasticsearch.go","lineNumber":30,"sourceCode":"\tes \"github.com/elastic/go-elasticsearch/v8\"\n\t\"github.com/elastic/go-elasticsearch/v8/esapi\"\n\t\"go.opentelemetry.io/otel/attribute\"\n\t\"go.opentelemetry.io/otel/trace\"\n)\n\nconst (\n\tstatusDown     = \"DOWN\"\n\tstatusUp       = \"UP\"\n\tdefaultTimeout = 5 * time.Second\n)\n\nvar (\n\terrEmptyIndex        = errors.New(\"index name cannot be empty\")\n\terrEmptyDocumentID   = errors.New(\"document ID cannot be empty\")\n\terrEmptyQuery        = errors.New(\"query cannot be empty\")\n\terrEmptyOperations   = errors.New(\"operations cannot be empty\")\n\terrHealthCheckFailed = errors.New(\"elasticsearch health check failed\")\n\terrOperation         = errors.New(\"elasticsearch operation error\")\n\terrMarshaling        = errors.New(\"error marshaling data\")\n\terrParsingResponse   = errors.New(\"error parsing response\")\n\terrResponse          = errors.New(\"invalid elasticsearch response\")\n\terrEncodingOperation = errors.New(\"error encoding operation\")\n)\n\n// Config holds the configuration for connecting to Elasticsearch.\ntype Config struct {\n\tAddresses []string\n\tUsername  string\n\tPassword  string\n}\n\n// Client represents the Elasticsearch client.\ntype Client struct {\n\tconfig  Config\n\tclient  *es.Client\n\tlogger  Logger","sourceCodeStart":12,"sourceCodeEnd":48,"githubUrl":"https://github.com/gofr-dev/gofr/blob/187eb24962502e91f1fee856230670958b66e89c/pkg/gofr/datasource/elasticsearch/elasticsearch.go#L12-L48","documentation":"In extractNestedClaim, each dot-separated segment must resolve to a map (map[string]any or jwt.MapClaims). If the current value is some other type (string, number, array, bool), the library wraps errInvalidClaimStructure with the traversed prefix path. This is a shape error: a mid-path segment is a scalar where a nested object is required.","triggerScenarios":"Path \"permissions.role\" where claims[\"permissions\"] is a JSON string or array rather than an object; intermediate segment exists but holds a scalar (e.g. \"meta.version.name\" where meta.version is \"1.2\").","commonSituations":"IdP stores permissions as a JSON-encoded string instead of an object; schema drift after an auth-service refactor; misremembered claim layout in config.","solutions":["Fix the token issuer so intermediate segments are JSON objects","Adjust the path to match the real structure (e.g. treat a JSON-string claim by decoding it separately rather than traversing it)","Pre-validate token payload shape with a test fixture asserting the nested object structure"],"exampleFix":"// before\n{\"permissions\": \"read,write\"} // path: permissions.role\n// after\n{\"permissions\": {\"role\": \"admin\"}}","handlingStrategy":"type-guard","validationCode":"node, ok := claims[\"permissions\"]\nif !ok { return fmt.Errorf(\"permissions missing\") }\nif _, ok := node.(map[string]any); !ok {\n    return fmt.Errorf(\"permissions must be a nested object\")\n}","typeGuard":"func isNestedMap(v any) bool {\n    switch v.(type) {\n    case map[string]any, jwt.MapClaims:\n        return true\n    }\n    return false\n}","tryCatchPattern":"v, err := extractClaimValue(claims, \"permissions.role\")\nif errors.Is(err, errInvalidClaimStructure) {\n    // token shape invalid: reject with 401 and log prefix\n}","preventionTips":["Assert intermediate claim segments are JSON objects in token fixtures","If the IdP emits JSON-encoded strings, decode them explicitly before traversal","Pin and review auth-server schema changes with contract tests"],"tags":["jwt","rbac","type-mismatch"],"backgroundTag":"jwt-claim-type-mismatch","analyzedSha":"187eb24962502e91f1fee856230670958b66e89c","analyzedAt":"2026-09-01T20:34:54.554Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-08T10:18:20.063Z"}