You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
fix(errors): extract API error body from GenericOpenAPIError
The datadog-api-client-go library consumes http.Response.Body during
deserialization and stores the raw bytes in GenericOpenAPIError.ErrorBody.
The error handling in metrics and logs commands was trying to re-read
http.Response.Body via io.ReadAll, which always returned empty data
since the body was already consumed. This silently discarded the actual
API error details, making 400/4xx errors impossible to diagnose.
- Add extractAPIErrorBody() helper that uses errors.As to extract the
response body from GenericOpenAPIError
- Replace all 10 broken io.ReadAll(r.Body) calls in cmd/metrics.go and
cmd/logs_simple.go with the new helper
- Enhance formatAPIError() to include the API response body when
available
- Add tests for extractAPIErrorBody and formatAPIError body inclusion
Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
returnfmt.Errorf("failed to search logs: %w\nStatus: %d\nAPI Response: %s\n\nRequest Details:\n- Query: %s\n- From: %s UTC (parsed from: %s)\n- To: %s UTC (parsed from: %s)\n- Limit: %d\n\nTroubleshooting:\n- Verify your time range is valid\n- Check that your query syntax is correct\n- Ensure you have proper permissions",
returnfmt.Errorf("failed to aggregate logs: %w\nStatus: %d\nAPI Response: %s\n\nRequest Details:\n- Query: %s\n- Compute: %s (parsed as: aggregation=%q, metric=%q)\n- Group By: %s\n- From: %s UTC (parsed from: %s)\n- To: %s UTC (parsed from: %s)\n- Limit: %d\n\nTroubleshooting:\n- Verify the aggregation function is supported\n- Ensure the metric field exists in your logs (e.g., @duration, @bytes)\n- Check your query syntax\n- Verify your time range is valid",
returnfmt.Errorf("failed to query metrics: %w\nStatus: %d\nAPI Response: %s\n\nRequest Details:\n- Query: %s\n- From: %s (Unix: %d)\n- To: %s (Unix: %d)\n\nTroubleshooting:\n- Verify your query syntax is correct (e.g., avg:metric.name{filter})\n- Check that the time range is valid\n- Ensure the metric exists and has data in the specified time range\n- Confirm you have proper permissions to access the metric",
returnfmt.Errorf("failed to list metrics: %w\nStatus: %d\nAPI Response: %s\n\nRequest Details:\n- Filter: %s\n- From: %s (Unix: %d)\n\nTroubleshooting:\n- Check that your filter pattern is valid\n- Verify you have permissions to list metrics",
returnfmt.Errorf("failed to get metric metadata: %w\nStatus: %d\nAPI Response: %s\n\nMetric: %s\n\nTroubleshooting:\n- Verify the metric name is correct\n- Ensure the metric exists in your account\n- Check that you have permissions to view metadata",
684
-
err, r.StatusCode, string(bodyBytes), metricName)
683
+
err, r.StatusCode, apiBody, metricName)
685
684
}
686
685
returnfmt.Errorf("failed to get metric metadata: %w (status: %d)", err, r.StatusCode)
returnfmt.Errorf("failed to update metric metadata: %w\nStatus: %d\nAPI Response: %s\n\nMetric: %s\n\nTroubleshooting:\n- Verify the metric name is correct\n- Check that the metadata values are valid (unit, type, etc.)\n- Ensure you have permissions to update metadata",
740
-
err, r.StatusCode, string(bodyBytes), metricName)
739
+
err, r.StatusCode, apiBody, metricName)
741
740
}
742
741
returnfmt.Errorf("failed to update metric metadata: %w (status: %d)", err, r.StatusCode)
returnfmt.Errorf("failed to submit metrics: %w\nStatus: %d\nAPI Response: %s\n\nRequest Details:\n- Metric: %s\n- Value: %f\n- Type: %s\n- Timestamp: %d\n- Tags: %v\n\nTroubleshooting:\n- Verify the metric name follows naming conventions (lowercase, dots/underscores)\n- Check that the metric type is valid (gauge, count, rate)\n- Ensure your API key has permission to submit metrics\n- Verify tags are in key:value format",
0 commit comments