{"record":{"id":"a5364f8c923b75f7","repo":"gofr-dev/gofr","slug":"error-encoding-operation","errorCode":null,"errorMessage":"error encoding operation","messagePattern":"error encoding operation","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"pkg/gofr/datasource/elasticsearch/elasticsearch.go","lineNumber":34,"sourceCode":")\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\n\tmetrics Metrics\n\ttracer  trace.Tracer\n}\n","sourceCodeStart":16,"sourceCodeEnd":52,"githubUrl":"https://github.com/gofr-dev/gofr/blob/187eb24962502e91f1fee856230670958b66e89c/pkg/gofr/datasource/elasticsearch/elasticsearch.go#L16-L52","documentation":"errEncodingOperation (\"error encoding operation\") is returned only by Bulk when json.NewEncoder fails to encode one of the operation maps into the bulk request buffer. Since a map[string]any should always be JSON-encodable in principle, this surfaces when an operation contains values Go's json encoder refuses to serialize, such as channels, funcs, complex numbers, or cyclically-referenced values hidden inside nested any fields.","triggerScenarios":"Calling Bulk(ctx, operations) where any entry in the []map[string]any slice contains an unencodable value: a func, chan, complex number, or a value whose MarshalJSON method returns an error.","commonSituations":"Developers hit this after building bulk operation maps dynamically, e.g. embedding a callback or channel by mistake, or when a custom MarshalJSON implementation panics/errors on certain data.","solutions":["Inspect the operations slice and remove/replace non-JSON-encodable values (func, chan, complex).","If values come from another system, sanitize/convert them to basic JSON types (string, number, bool, nil, slice, map) before passing to Bulk.","Check any custom MarshalJSON implementations on nested values for error paths."],"exampleFix":"// before\nop := map[string]any{\"index\": map[string]any{\"_id\": id}, \"payload\": someStructWithChan}\n// after\nop := map[string]any{\"index\": map[string]any{\"_id\": id}, \"payload\": toPlainJSONValue(someStructWithChan)}","handlingStrategy":"validation","validationCode":"func validateEncodable(ops []map[string]any) error {\n    for i, op := range ops {\n        if _, err := json.Marshal(op); err != nil {\n            return fmt.Errorf(\"operation %d not JSON-encodable: %w\", i, err)\n        }\n    }\n    return nil\n}","typeGuard":null,"tryCatchPattern":"if err := validateEncodable(operations); err != nil {\n    return fmt.Errorf(\"skipping bulk, invalid operations: %w\", err)\n}\nresult, err := client.Bulk(ctx, operations)\nif err != nil {\n    return fmt.Errorf(\"bulk failed: %w\", err)\n}","preventionTips":["Build operation maps only from JSON-primitive values; sanitize external input first.","Add a unit test that marshals representative operations before they reach Bulk.","Avoid embedding channels/funcs in generic any fields when assembling payloads."],"tags":["elasticsearch","json-encoding","bulk-api"],"backgroundTag":"json-marshal-failed","analyzedSha":"187eb24962502e91f1fee856230670958b66e89c","analyzedAt":"2026-09-01T20:34:54.554Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-08T05:18:18.240Z"}