googleapis/mcp-toolbox · error

request failed with status %s: %s

Error message

request failed with status %s: %s

What it means

The Database Insights API returned a non-200 status for queryStats:fetch. The error includes the HTTP status line and the raw response body so the caller can see the API's error payload (e.g. permission denied, resource not found).

Source

Thrown at internal/sources/databaseinsights/databaseinsights.go:385

	if err != nil {
		return nil, fmt.Errorf("failed to marshal request: %w", err)
	}

	httpReq, err := http.NewRequestWithContext(ctx, "POST", url, bytes.NewBuffer(bodyBytes))
	if err != nil {
		return nil, fmt.Errorf("failed to create http request: %w", err)
	}
	httpReq.Header.Set("Content-Type", "application/json")

	resp, err := s.httpClient.Do(httpReq)
	if err != nil {
		return nil, fmt.Errorf("failed to execute request: %w", err)
	}
	defer resp.Body.Close()

	if resp.StatusCode != http.StatusOK {
		respBody, _ := io.ReadAll(resp.Body)
		return nil, fmt.Errorf("request failed with status %s: %s", resp.Status, string(respBody))
	}

	var fetchResp FetchQueryStatsResponse
	if err := json.NewDecoder(resp.Body).Decode(&fetchResp); err != nil {
		return nil, fmt.Errorf("failed to decode response: %w", err)
	}

	return &fetchResp, nil
}

// FetchWaitEventStats executes the FetchWaitEventStats REST API method.
func (s *Source) FetchWaitEventStats(ctx context.Context, req *FetchWaitEventStatsRequest) (*FetchWaitEventStatsResponse, error) {
	url := fmt.Sprintf("%s/v1beta/%s/waitEventStats:fetch", s.getEndpointForParent(req.Parent), req.Parent)

	bodyBytes, err := json.Marshal(req)
	if err != nil {
		return nil, fmt.Errorf("failed to marshal request: %w", err)
	}

View on GitHub (pinned to 8cc6e09de2)

Solutions

  1. Read the response body in the error — it contains the Google API error explanation
  2. Verify IAM permissions (e.g. roles/databaseinsights.viewer or cloudplatform scope authorization) on the parent project
  3. Confirm the API (databaseinsights.googleapis.com) is enabled and the parent project/location/instance exist
  4. Check req.Parent format: projects/PROJECT/locations/REGION (and any required segments)
  5. Retry with backoff on 5xx; fix request/permissions on 4xx

Example fix

// before
req.Parent = "projects/wrong-proj/locations/us-central1" // 403/404
// after
req.Parent = "projects/my-actual-project/locations/us-central1"
Defensive patterns

Strategy: try-catch

Validate before calling

// preflight IAM check with the same credentials
creds, _ := google.FindDefaultCredentials(ctx, "https://www.googleapis.com/auth/cloud-platform")
tok, _ := creds.TokenSource.Token()
req2, _ := http.NewRequestWithContext(ctx, "GET", "https://databaseinsights.googleapis.com/v1beta/"+parent, nil)
req2.Header.Set("Authorization", "Bearer "+tok.AccessToken)
resp, _ := http.DefaultClient.Do(req2)
if resp.StatusCode != 200 { return fmt.Errorf("preflight failed: %s", resp.Status) }

Try / catch

resp, err := src.FetchQueryStats(ctx, req)
if err != nil {
  var apiErr struct{ Status string; Body string }
  if strings.Contains(err.Error(), "request failed with status") {
    if strings.Contains(err.Error(), "403") { /* fix IAM permissions */ }
    if strings.Contains(err.Error(), "404") { /* verify parent resource exists */ }
    if strings.Contains(err.Error(), " 5") { /* retry with backoff */ }
  }
  return err
}

Prevention

When it happens

Trigger: Server responded 4xx/5xx: invalid or unauthorized parent resource, missing databaseinsights.roles/viewer permissions, nonexistent project/location, malformed request rejected by the API, or API outage (5xx).

Common situations: Service account lacking the required Database Insights IAM roles; typo in project ID or region in req.Parent; API not enabled on the project; querying an instance that doesn't exist.

Related errors


AI-assisted analysis of googleapis/mcp-toolbox@8cc6e09de2 (2026-09-05). Data as JSON: /api/errors/f663222555aad00b. Report an issue: GitHub.