If we get an unexpected HTTP status code, print the body in error

This commit is contained in:
Steven Polley 2018-08-25 18:17:59 -06:00
parent a12e38597f
commit 460b0a8aa6
1 changed files with 5 additions and 3 deletions

View File

@ -116,14 +116,16 @@ func (cw *Site) BuildURL(restAction string) (*url.URL, error) {
//Checks for HTTP errors, and if all looks good, returns the body of the HTTP response as a byte slice
//TBD: Needs to accept 201 and 204 (returned for Create and Delete operations)
func getHTTPResponseBody(resp *http.Response) ([]byte, error) {
if (resp.StatusCode != http.StatusOK) && (resp.StatusCode != http.StatusCreated) && (resp.StatusCode != http.StatusNoContent) {
return nil, fmt.Errorf("cw api returned unexpected http status - %s", resp.Status)
}
defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
return nil, fmt.Errorf("could not read response body of request")
}
if (resp.StatusCode != http.StatusOK) && (resp.StatusCode != http.StatusCreated) && (resp.StatusCode != http.StatusNoContent) {
return nil, fmt.Errorf("cw api returned unexpected http status - %s: response body is '%s'", resp.Status, string(body))
}
return body, nil
}