diff --git a/mandrill.go b/mandrill.go index 6845bcb..1716688 100644 --- a/mandrill.go +++ b/mandrill.go @@ -320,7 +320,10 @@ func (c *Client) sendApiRequest(data interface{}, path string) (body []byte, err if resp.StatusCode >= 400 { resError := &Error{} - err = json.Unmarshal(body, resError) + if err = json.Unmarshal(body, resError); err != nil { + return body, err + } + return body, resError } diff --git a/mandrill_test.go b/mandrill_test.go index 109b9f6..594ccdf 100644 --- a/mandrill_test.go +++ b/mandrill_test.go @@ -1,6 +1,7 @@ package mandrill import ( + "encoding/json" "fmt" "net/http" "net/http/httptest" @@ -82,6 +83,18 @@ func Test_MessagesSendTemplate_Fail(t *testing.T) { expect(t, reflect.DeepEqual(correctResponse, err), true) } +func Test_MessagesSendTemplate_Fail_Invalid_JSON(t *testing.T) { + server, m := testTools(400, ``) + defer server.Close() + responses, err := m.MessagesSendTemplate(&Message{}, "cheese", map[string]string{"name": "bob"}) + + expect(t, len(responses), 0) + + correctResponse := json.Unmarshal([]byte(` `), struct{}{}) + + expect(t, reflect.DeepEqual(correctResponse, err), true) +} + // MessagesSend ////////// func Test_MessageSend_Success(t *testing.T) {