Skip to content

Commit

Permalink
show response from API when server returned non 200 code
Browse files Browse the repository at this point in the history
  • Loading branch information
nanjiangshu committed Sep 12, 2024
1 parent 836da4a commit cbe7082
Showing 1 changed file with 10 additions and 11 deletions.
21 changes: 10 additions & 11 deletions sda-admin/helpers/helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,19 +29,18 @@ func GetBody(url, token string) ([]byte, error) {
if err != nil {
return nil, fmt.Errorf("failed to get response, reason: %v", err)
}

// Check the status code
if res.StatusCode != http.StatusOK {
return nil, fmt.Errorf("server returned status %d", res.StatusCode)
}
defer res.Body.Close()

// Read the response body
resBody, err := io.ReadAll(res.Body)
if err != nil {
return nil, fmt.Errorf("failed to read response body, reason: %v", err)
}

defer res.Body.Close()
// Check the status code
if res.StatusCode != http.StatusOK {
return nil, fmt.Errorf("server returned status %d: %s", res.StatusCode, string(resBody))
}

return resBody, nil
}
Expand Down Expand Up @@ -69,17 +68,17 @@ func PostReq(url, token string, jsonBody []byte) ([]byte, error) {
}
defer res.Body.Close()

// Check the status code
if res.StatusCode != http.StatusOK {
return nil, fmt.Errorf("server returned status %d", res.StatusCode)
}

// Read the response body
resBody, err := io.ReadAll(res.Body)
if err != nil {
return nil, fmt.Errorf("failed to read response body, reason: %v", err)
}

// Check the status code
if res.StatusCode != http.StatusOK {
return nil, fmt.Errorf("server returned status %d: %s", res.StatusCode, string(resBody))
}

return resBody, nil
}

Expand Down

0 comments on commit cbe7082

Please sign in to comment.