Skip to content

Commit

Permalink
fix: don't discard a response body even if it isn't JSON (#119)
Browse files Browse the repository at this point in the history
  • Loading branch information
kzys authored Nov 12, 2024
1 parent c51b4e8 commit 94cfcf4
Showing 1 changed file with 5 additions and 4 deletions.
9 changes: 5 additions & 4 deletions flaps/flaps.go
Original file line number Diff line number Diff line change
Expand Up @@ -302,6 +302,7 @@ func (f *Client) getCaveatNames() ([]string, error) {
return caveatNames, nil
}

// handleAPIError returns an error based on the status code and response body.
func handleAPIError(statusCode int, responseBody []byte) error {
switch statusCode / 100 {
case 1, 3:
Expand All @@ -311,10 +312,10 @@ func handleAPIError(statusCode int, responseBody []byte) error {
Error string `json:"error"`
Message string `json:"message,omitempty"`
}{}
if err := json.Unmarshal(responseBody, &apiErr); err != nil {
return fmt.Errorf("request returned non-2xx status, %d", statusCode)
}
if apiErr.Message != "" {
jsonErr := json.Unmarshal(responseBody, &apiErr)
if jsonErr != nil {
return fmt.Errorf("request returned non-2xx status: %d: %s", statusCode, string(responseBody))
} else if apiErr.Message != "" {
return fmt.Errorf("%s", apiErr.Message)
}
return errors.New(apiErr.Error)
Expand Down

0 comments on commit 94cfcf4

Please sign in to comment.