gofr-dev/gofr · error
%w: query: %w
Error message
%w: query: %w
What it means
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.
Source
Thrown at pkg/gofr/datasource/elasticsearch/elasticsearch.go:196
// Search executes a query against one or more indices.
// Returns the entire response JSON as a map.
func (c *Client) Search(ctx context.Context, indices []string, query map[string]any) (map[string]any, error) {
if len(indices) == 0 {
return nil, errEmptyIndex
}
if len(query) == 0 {
return nil, errEmptyQuery
}
start := time.Now()
tracedCtx, span := c.addTrace(ctx, "search", indices, "")
body, err := json.Marshal(query)
if err != nil {
return nil, fmt.Errorf("%w: query: %w", errMarshaling, err)
}
req := esapi.SearchRequest{
Index: indices,
Body: bytes.NewReader(body),
}
res, err := req.Do(tracedCtx, c.client)
if err != nil {
return nil, fmt.Errorf("%w: executing search: %w", errOperation, err)
}
defer res.Body.Close()
if res.IsError() {
return nil, fmt.Errorf("%w: %s", errResponse, res.String())
}
View on GitHub (pinned to 187eb24962)
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.
Example fix
// before
query := map[string]any{"query": map[string]any{"match": map[string]any{field: valueChan}}}
res, err := client.Search(ctx, indices, query)
// after
query := map[string]any{"query": map[string]any{"match": map[string]any{field: fmt.Sprintf("%v", value)}}}
res, err := client.Search(ctx, indices, query) Defensive patterns
Strategy: validation
Validate before calling
func validateQuery(query map[string]any) error {
if len(query) == 0 {
return errors.New("query cannot be empty")
}
if _, err := json.Marshal(query); err != nil {
return fmt.Errorf("query not JSON-encodable: %w", err)
}
return nil
} Try / catch
if err := validateQuery(query); err != nil {
return nil, fmt.Errorf("invalid search query: %w", err)
}
result, err := client.Search(ctx, indices, query)
if err != nil {
return nil, fmt.Errorf("search failed: %w", err)
} Prevention
- 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.
When it happens
Trigger: Calling Search(ctx, indices, query) where the query map contains a non-JSON-encodable value or a nested value whose MarshalJSON returns an error.
Common situations: 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.
Understand the failure class
Background: json.Marshal / "failed to marshal" errors in Go: why "unsupported type" happens and how to fix it — this error's family across 22 libraries.
Related errors
- %w: settings: %w
- %w: executing search: %w
- invalid elasticsearch response
- error encoding operation
- %w: creating index: %w
AI-assisted analysis of gofr-dev/gofr@187eb24962 (2026-09-01).
Data as JSON: /api/errors/fb34496728315343.
Report an issue: GitHub.