{"record":{"id":"fb34496728315343","repo":"gofr-dev/gofr","slug":"w-query-w","errorCode":null,"errorMessage":"%w: query: %w","messagePattern":"%w: query: %w","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"pkg/gofr/datasource/elasticsearch/elasticsearch.go","lineNumber":196,"sourceCode":"\n// Search executes a query against one or more indices.\n// Returns the entire response JSON as a map.\nfunc (c *Client) Search(ctx context.Context, indices []string, query map[string]any) (map[string]any, error) {\n\tif len(indices) == 0 {\n\t\treturn nil, errEmptyIndex\n\t}\n\n\tif len(query) == 0 {\n\t\treturn nil, errEmptyQuery\n\t}\n\n\tstart := time.Now()\n\n\ttracedCtx, span := c.addTrace(ctx, \"search\", indices, \"\")\n\n\tbody, err := json.Marshal(query)\n\tif err != nil {\n\t\treturn nil, fmt.Errorf(\"%w: query: %w\", errMarshaling, err)\n\t}\n\n\treq := esapi.SearchRequest{\n\t\tIndex: indices,\n\t\tBody:  bytes.NewReader(body),\n\t}\n\n\tres, err := req.Do(tracedCtx, c.client)\n\tif err != nil {\n\t\treturn nil, fmt.Errorf(\"%w: executing search: %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","sourceCodeStart":178,"sourceCodeEnd":214,"githubUrl":"https://github.com/gofr-dev/gofr/blob/187eb24962502e91f1fee856230670958b66e89c/pkg/gofr/datasource/elasticsearch/elasticsearch.go#L178-L214","documentation":"This is errMarshaling wrapped around a json.Marshal failure of the query map in Search. The search body could not be converted to JSON, so no request is sent. Causes include non-encodable values (func, chan, complex) inside the query map or a custom json.Marshaler returning an error.","triggerScenarios":"Calling Search(ctx, indices, query) where the query map contains a non-JSON-encodable value or a nested value whose MarshalJSON returns an error.","commonSituations":"Developers hit this when building queries dynamically from user data that leaks in unsupported Go values, or when reusing query-builder structs with buggy custom marshaling.","solutions":["Inspect the query map for func/chan/complex values and replace them with JSON primitives.","Reproduce with json.Marshal(query) locally to read the exact encoder error.","Sanitize dynamically built queries before passing them to Search.","Fix any custom MarshalJSON implementations on values inside the query."],"exampleFix":"// before\nquery := map[string]any{\"query\": map[string]any{\"match\": map[string]any{field: valueChan}}}\nres, err := client.Search(ctx, indices, query)\n// after\nquery := map[string]any{\"query\": map[string]any{\"match\": map[string]any{field: fmt.Sprintf(\"%v\", value)}}}\nres, err := client.Search(ctx, indices, query)","handlingStrategy":"validation","validationCode":"func validateQuery(query map[string]any) error {\n    if len(query) == 0 {\n        return errors.New(\"query cannot be empty\")\n    }\n    if _, err := json.Marshal(query); err != nil {\n        return fmt.Errorf(\"query not JSON-encodable: %w\", err)\n    }\n    return nil\n}","typeGuard":null,"tryCatchPattern":"if err := validateQuery(query); err != nil {\n    return nil, fmt.Errorf(\"invalid search query: %w\", err)\n}\nresult, err := client.Search(ctx, indices, query)\nif err != nil {\n    return nil, fmt.Errorf(\"search failed: %w\", err)\n}","preventionTips":["Build queries with typed structs or vetted builder helpers, not raw dynamic maps.","Unit-test json.Marshal on representative queries including user-derived values.","Sanitize user input into JSON primitives before embedding it in a query."],"tags":["elasticsearch","json-marshal","search"],"backgroundTag":"json-marshal-failed","analyzedSha":"187eb24962502e91f1fee856230670958b66e89c","analyzedAt":"2026-09-01T20:34:54.554Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-08T10:18:20.063Z"}