Check status before decoding Google Analytics accounts
ListGoogleAnalyticsOrganizations decoded the response body into the success struct before inspecting the HTTP status, unlike every other lister in the file. Check the status first so a non-2xx no longer wastes a decode against an error body and the ordering matches the sibling functions. Signed-off-by: Aurélien Sibiril <81782+aureliensibiril@users.noreply.github.com>
This commit is contained in:
@@ -508,6 +508,12 @@ func ListGoogleAnalyticsOrganizations(ctx context.Context, httpClient *http.Clie
|
||||
return nil, fmt.Errorf("cannot fetch google analytics accounts: %w", err)
|
||||
}
|
||||
|
||||
if resp.StatusCode != http.StatusOK {
|
||||
_ = resp.Body.Close()
|
||||
|
||||
return nil, fmt.Errorf("cannot fetch google analytics accounts: unexpected status %d", resp.StatusCode)
|
||||
}
|
||||
|
||||
var out struct {
|
||||
Accounts []struct {
|
||||
Name string `json:"name"`
|
||||
@@ -517,13 +523,8 @@ func ListGoogleAnalyticsOrganizations(ctx context.Context, httpClient *http.Clie
|
||||
}
|
||||
|
||||
decodeErr := json.NewDecoder(resp.Body).Decode(&out)
|
||||
status := resp.StatusCode
|
||||
_ = resp.Body.Close()
|
||||
|
||||
if status != http.StatusOK {
|
||||
return nil, fmt.Errorf("cannot fetch google analytics accounts: unexpected status %d", status)
|
||||
}
|
||||
|
||||
if decodeErr != nil {
|
||||
return nil, fmt.Errorf("cannot decode google analytics accounts response: %w", decodeErr)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user