fix(errors): extract API error body from GenericOpenAPIError#52
Merged
Conversation
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>
📊 Test Coverage ReportThreshold: 80% ✅ Coverage by Package📈 Coverage Status: ✅ PASSED - Coverage meets minimum threshold Updated for commit cab8890 |
- Add wrapped GenericOpenAPIError test case to verify errors.As unwrapping - Expand formatAPIError doc comment to describe body extraction behavior - Document why inline error handlers bypass formatAPIError - Fix pre-existing string(rune(tt.code)) producing Unicode garbage in subtest names Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
platinummonkey
approved these changes
Feb 12, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem
datadog-api-client-goconsumeshttp.Response.Bodyduring deserialization and stores the raw bytes indatadog.GenericOpenAPIError.formatAPIError()re-readhttp.Response.Body, which was always empty by that point. Users saw only:No indication of what went wrong.
Fix
Extract the response body from
GenericOpenAPIError.Body()instead of re-reading the drainedhttp.Response.Body. API errors now surface the actual server response:Changes
extractAPIErrorBody()— unwrapsGenericOpenAPIErrorand returns the stored body (cmd/root.go:264)extractAPIErrorBody()fromformatAPIError()to include the body in all API error paths (cmd/root.go:291)cmd/logs_simple.goandcmd/metrics.gofor direct error handlingTesting
extractAPIErrorBody: body present, empty body, nil body, non-API error, nil error (cmd/root_test.go:323)formatAPIErrorincludes the response body in output (cmd/root_test.go:375)pup metrics query --query="avg:invalid.metric" --from="1h"shows the full API parse error🤖 Generated with Claude Code