{"record":{"id":"b4ee486a0523f855","repo":"googleapis/mcp-toolbox","slug":"failed-to-read-response-body-w","errorCode":null,"errorMessage":"failed to read response body: %w","messagePattern":"failed to read response body: %w","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"internal/sources/cloudmonitoring/cloud_monitoring.go","lineNumber":163,"sourceCode":"\tif err != nil {\n\t\treturn nil, err\n\t}\n\n\tq := req.URL.Query()\n\tq.Add(\"query\", query)\n\treq.URL.RawQuery = q.Encode()\n\n\treq.Header.Set(\"User-Agent\", s.UserAgent())\n\n\tresp, err := s.Client().Do(req)\n\tif err != nil {\n\t\treturn nil, err\n\t}\n\tdefer resp.Body.Close()\n\n\tbody, err := io.ReadAll(resp.Body)\n\tif err != nil {\n\t\treturn nil, fmt.Errorf(\"failed to read response body: %w\", err)\n\t}\n\n\tif resp.StatusCode != http.StatusOK {\n\t\treturn nil, fmt.Errorf(\"request failed: %s, body: %s\", resp.Status, string(body))\n\t}\n\n\tif len(body) == 0 {\n\t\treturn nil, nil\n\t}\n\n\tvar result map[string]any\n\tif err := json.Unmarshal(body, &result); err != nil {\n\t\treturn nil, fmt.Errorf(\"failed to unmarshal json: %w, body: %s\", err, string(body))\n\t}\n\n\treturn result, nil\n}\n","sourceCodeStart":145,"sourceCodeEnd":181,"githubUrl":"https://github.com/googleapis/mcp-toolbox/blob/8cc6e09de2ad7b8bffc77751799585a1401a48eb/internal/sources/cloudmonitoring/cloud_monitoring.go#L145-L181","documentation":"RunQuery performs an HTTP call to the Cloud Monitoring API and reads the entire response body with io.ReadAll. If that read fails (connection reset mid-response, truncated body, timeouts, interrupted transfer), the error is wrapped with this message.","triggerScenarios":"Executing a monitoring query where the HTTP response body cannot be fully read — server closes connection early, proxy truncates the response, or transient network failure during body transfer.","commonSituations":"Flaky networks or long-running queries hitting idle timeouts; corporate proxies/CDNs dropping large responses; server-side 5xx with abrupt connection termination.","solutions":["Retry the query; the failure is often transient","Set/raise HTTP client timeouts and keep-alive settings","Bypass or reconfigure intermediate proxies that truncate responses","Check Cloud Monitoring API status and response size (narrow the query time window or filters)"],"exampleFix":"// before\nbody, err := io.ReadAll(resp.Body)\nif err != nil { return nil, fmt.Errorf(\"failed to read response body: %w\", err) }\n// after\n// retry the request with backoff\nclient.Timeout = 120 * time.Second\nbody, err := io.ReadAll(resp.Body)","handlingStrategy":"retry","validationCode":null,"typeGuard":null,"tryCatchPattern":"var result map[string]any\nerr := retry(3, backoff, func() error {\n    result, err = src.RunQuery(ctx, query)\n    if err != nil && strings.Contains(err.Error(), \"failed to read response body\") {\n        return err // retryable\n    }\n    return nil\n})","preventionTips":["Set generous HTTP client timeouts for large queries","Narrow query time ranges to reduce response size","Check proxy idle-timeout settings","Monitor network reliability in the deployment environment"],"tags":["network","http","cloud-monitoring"],"backgroundTag":"http-response-read-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"}