Skip to content

Commit

Permalink
Log response error status code and body (#166)
Browse files Browse the repository at this point in the history
  • Loading branch information
kontrollanten authored and bmoffatt committed Mar 15, 2019
1 parent b3e2820 commit 08d251d
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 4 deletions.
6 changes: 4 additions & 2 deletions cfn/response.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,9 @@ package cfn
import (
"bytes"
"encoding/json"
"errors"
"fmt"
"io/ioutil"
"log"
"net/http"
)

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

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

return nil
Expand Down
9 changes: 7 additions & 2 deletions cfn/response_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ package cfn

import (
"bytes"
"fmt"
"io"
"net/http"
"testing"
Expand Down Expand Up @@ -68,15 +69,19 @@ func TestRequestForbidden(t *testing.T) {
url: "http://pre-signed-S3-url-for-response",
}

sc := http.StatusForbidden
client := &mockClient{
DoFunc: func(req *http.Request) (*http.Response, error) {
assert.NotContains(t, req.Header, "Content-Type")
return &http.Response{
StatusCode: http.StatusForbidden,
StatusCode: sc,
Body: nopCloser{bytes.NewBufferString("")},
}, nil
},
}

assert.Error(t, r.sendWith(client))
s := r.sendWith(client)
if assert.Error(t, s) {
assert.Equal(t, fmt.Errorf("invalid status code. got: %d", sc), s)
}
}

0 comments on commit 08d251d

Please sign in to comment.