From 94cfcf4bb7436ad80c9749c9059614acb71d89e3 Mon Sep 17 00:00:00 2001 From: Kazuyoshi Kato Date: Tue, 12 Nov 2024 15:52:28 -0800 Subject: [PATCH] fix: don't discard a response body even if it isn't JSON (#119) --- flaps/flaps.go | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/flaps/flaps.go b/flaps/flaps.go index 4dd0739..60d0159 100644 --- a/flaps/flaps.go +++ b/flaps/flaps.go @@ -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: @@ -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)