diff --git a/client/client.go b/client/client.go index 9125b29..11c4a70 100644 --- a/client/client.go +++ b/client/client.go @@ -115,7 +115,7 @@ func Do(request *http.Request) (*jsh.Document, *http.Response, error) { request.Header.Set("Content-Length", strconv.Itoa(int(request.ContentLength))) client := &http.Client{} - httpResponse, clientErr := client.Do(request) + response, clientErr := client.Do(request) if clientErr != nil { return nil, nil, fmt.Errorf( @@ -123,14 +123,33 @@ func Do(request *http.Request) (*jsh.Document, *http.Response, error) { ) } - if request.Method == "DELETE" { - return nil, httpResponse, nil + doc, parseErr := ParseResponse(response) + if parseErr != nil { + return nil, response, fmt.Errorf("Error parsing response: %s", parseErr.Error()) } - document, err := Document(httpResponse) + return doc, response, parseErr +} + +// ParseResponse handles parsing an HTTP response into a JSON Document if +// possible +func ParseResponse(response *http.Response) (*jsh.Document, error) { + + skipCodes := []int{ + http.StatusNoContent, + http.StatusNotFound, + } + + for _, code := range skipCodes { + if code == response.StatusCode { + return nil, nil + } + } + + document, err := Document(response) if err != nil { - return nil, httpResponse, err + return nil, err } - return document, httpResponse, nil + return document, nil } diff --git a/client/client_test.go b/client/client_test.go index 0c8b5f3..d87c807 100644 --- a/client/client_test.go +++ b/client/client_test.go @@ -1,6 +1,7 @@ package jsc import ( + "net/http" "net/url" "testing" @@ -42,11 +43,27 @@ func TestClientRequest(t *testing.T) { }) } +func TestParseResponse(t *testing.T) { + + Convey("ParseResponse", t, func() { + + response := &http.Response{ + StatusCode: http.StatusNotFound, + } + + Convey("404 response parsing should not return a 406 error", func() { + doc, err := ParseResponse(response) + So(doc, ShouldBeNil) + So(err, ShouldBeNil) + }) + }) +} + func TestResponseParsing(t *testing.T) { Convey("Response Parsing Tests", t, func() { - Convey("->ParseObject()", func() { + Convey("Parse Object", func() { obj, objErr := jsh.NewObject("123", "test", map[string]string{"test": "test"}) So(objErr, ShouldBeNil) @@ -63,7 +80,7 @@ func TestResponseParsing(t *testing.T) { }) }) - Convey("->GetList()", func() { + Convey("Parse List", func() { obj, objErr := jsh.NewObject("123", "test", map[string]string{"test": "test"}) So(objErr, ShouldBeNil) diff --git a/error.go b/error.go index 499141b..ccab888 100644 --- a/error.go +++ b/error.go @@ -65,7 +65,7 @@ http://jsonapi.org/format/#error-objects type Error struct { Title string `json:"title"` Detail string `json:"detail"` - Status int `json:"status"` + Status int `json:"status,string"` Source struct { Pointer string `json:"pointer"` } `json:"source"`