Skip to content

Commit 08d251d

Browse files
kontrollantenbmoffatt
authored andcommitted
Log response error status code and body (#166)
1 parent b3e2820 commit 08d251d

File tree

2 files changed

+11
-4
lines changed

2 files changed

+11
-4
lines changed

cfn/response.go

+4-2
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,9 @@ package cfn
55
import (
66
"bytes"
77
"encoding/json"
8-
"errors"
8+
"fmt"
99
"io/ioutil"
10+
"log"
1011
"net/http"
1112
)
1213

@@ -73,7 +74,8 @@ func (r *Response) sendWith(client httpClient) error {
7374
res.Body.Close()
7475

7576
if res.StatusCode != 200 {
76-
return errors.New("invalid status code")
77+
log.Printf("StatusCode: %d\nBody: %v\n", res.StatusCode, string(body))
78+
return fmt.Errorf("invalid status code. got: %d", res.StatusCode)
7779
}
7880

7981
return nil

cfn/response_test.go

+7-2
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ package cfn
44

55
import (
66
"bytes"
7+
"fmt"
78
"io"
89
"net/http"
910
"testing"
@@ -68,15 +69,19 @@ func TestRequestForbidden(t *testing.T) {
6869
url: "http://pre-signed-S3-url-for-response",
6970
}
7071

72+
sc := http.StatusForbidden
7173
client := &mockClient{
7274
DoFunc: func(req *http.Request) (*http.Response, error) {
7375
assert.NotContains(t, req.Header, "Content-Type")
7476
return &http.Response{
75-
StatusCode: http.StatusForbidden,
77+
StatusCode: sc,
7678
Body: nopCloser{bytes.NewBufferString("")},
7779
}, nil
7880
},
7981
}
8082

81-
assert.Error(t, r.sendWith(client))
83+
s := r.sendWith(client)
84+
if assert.Error(t, s) {
85+
assert.Equal(t, fmt.Errorf("invalid status code. got: %d", sc), s)
86+
}
8287
}

0 commit comments

Comments
 (0)