From 460b0a8aa6a418687257d42eb09810b390f417f9 Mon Sep 17 00:00:00 2001 From: Steven Polley Date: Sat, 25 Aug 2018 18:17:59 -0600 Subject: [PATCH] If we get an unexpected HTTP status code, print the body in error --- 3.0/connectwise/requests.go | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/3.0/connectwise/requests.go b/3.0/connectwise/requests.go index 69accf4..533ac0a 100644 --- a/3.0/connectwise/requests.go +++ b/3.0/connectwise/requests.go @@ -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 }