{"record":{"id":"5645df6f4c511024","repo":"googleapis/mcp-toolbox","slug":"failed-to-marshal-request-w","errorCode":null,"errorMessage":"failed to marshal request: %w","messagePattern":"failed to marshal request: %w","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"internal/sources/databaseinsights/databaseinsights.go","lineNumber":368,"sourceCode":"// DatabaseIndexRecommendation represents recommendations for a specific database.\ntype DatabaseIndexRecommendation struct {\n\tDatabase             string                      `json:\"database\"`\n\tIndexRecommendations []IndexRecommendation       `json:\"indexRecommendations\"`\n\tQueryImprovements    map[string]QueryImprovement `json:\"queryImprovements\"`\n}\n\n// BatchQueryIndexRecommendationsResponse contains index recommendations.\ntype BatchQueryIndexRecommendationsResponse struct {\n\tDatabaseIndexRecommendations []DatabaseIndexRecommendation `json:\"databaseIndexRecommendations\"`\n}\n\n// FetchQueryStats executes the FetchQueryStats REST API method.\nfunc (s *Source) FetchQueryStats(ctx context.Context, req *FetchQueryStatsRequest) (*FetchQueryStatsResponse, error) {\n\turl := fmt.Sprintf(\"%s/v1beta/%s/queryStats:fetch\", s.getEndpointForParent(req.Parent), req.Parent)\n\n\tbodyBytes, err := json.Marshal(req)\n\tif err != nil {\n\t\treturn nil, fmt.Errorf(\"failed to marshal request: %w\", err)\n\t}\n\n\thttpReq, err := http.NewRequestWithContext(ctx, \"POST\", url, bytes.NewBuffer(bodyBytes))\n\tif err != nil {\n\t\treturn nil, fmt.Errorf(\"failed to create http request: %w\", err)\n\t}\n\thttpReq.Header.Set(\"Content-Type\", \"application/json\")\n\n\tresp, err := s.httpClient.Do(httpReq)\n\tif err != nil {\n\t\treturn nil, fmt.Errorf(\"failed to execute request: %w\", err)\n\t}\n\tdefer resp.Body.Close()\n\n\tif resp.StatusCode != http.StatusOK {\n\t\trespBody, _ := io.ReadAll(resp.Body)\n\t\treturn nil, fmt.Errorf(\"request failed with status %s: %s\", resp.Status, string(respBody))\n\t}","sourceCodeStart":350,"sourceCodeEnd":386,"githubUrl":"https://github.com/googleapis/mcp-toolbox/blob/8cc6e09de2ad7b8bffc77751799585a1401a48eb/internal/sources/databaseinsights/databaseinsights.go#L350-L386","documentation":"FetchQueryStats marshals the FetchQueryStatsRequest to JSON before POSTing it to the queryStats:fetch REST endpoint. If json.Marshal fails, this wrapped error is returned. In practice this is rare because the request struct contains only JSON-serializable fields.","triggerScenarios":"json.Marshal(req) returns an error — typically only if the request struct (or a field within it) contains an unsupported type such as a channel, func, or complex number.","commonSituations":"A code change introducing an unsupported field type into FetchQueryStatsRequest; a custom JSONMarshaler on a field returning an error; corrupted in-memory data passed in by a tool.","solutions":["Inspect the wrapped error to identify which field failed to marshal","Audit FetchQueryStatsRequest fields for unsupported types (chan, func, complex)","Validate the request payload constructed by the calling tool before invoking FetchQueryStats"],"exampleFix":"// before\nreq.Filter = someUnsupportedValue // e.g. a chan or func field\nresp, err := s.FetchQueryStats(ctx, req)\n// after\nreq.Filter = \"valid-string-filter\"\nresp, err := s.FetchQueryStats(ctx, req)","handlingStrategy":"try-catch","validationCode":"if b, err := json.Marshal(req); err != nil || len(b) == 0 {\n  return fmt.Errorf(\"invalid FetchQueryStatsRequest: %w\", err)\n}","typeGuard":null,"tryCatchPattern":"resp, err := src.FetchQueryStats(ctx, req)\nif err != nil {\n  if strings.Contains(err.Error(), \"failed to marshal request\") {\n    // log req fields, fix serialization before retrying\n  }\n  return err\n}","preventionTips":["Keep request structs limited to JSON-serializable types","Add unit tests marshaling all request variants","Validate tool parameters before building requests"],"tags":["json","serialization"],"backgroundTag":"json-marshal-failed","analyzedSha":"8cc6e09de2ad7b8bffc77751799585a1401a48eb","analyzedAt":"2026-09-05T01:10:36.887Z","contentChangedAt":"2026-09-05T01:10:36.887Z","schemaVersion":2},"datasetVersion":"2026-09-08T10:18:20.063Z"}