From db3273390f0c31cc45dd6b5e1f551fb95a97c569 Mon Sep 17 00:00:00 2001 From: Daniele Martinoli <86618610+dmartinol@users.noreply.github.com> Date: Tue, 27 Jun 2023 08:51:02 +0200 Subject: [PATCH 1/3] fix: parseResponseBody overrides original error code in case of unmarshalling error #673 --- middleware.go | 6 +++++- request_test.go | 48 ++++++++++++++++++++++++++++++++++++++++++++++++ resty_test.go | 7 +++++++ 3 files changed, 60 insertions(+), 1 deletion(-) diff --git a/middleware.go b/middleware.go index 4ffb9dbd..fe0c683b 100644 --- a/middleware.go +++ b/middleware.go @@ -9,6 +9,7 @@ import ( "errors" "fmt" "io" + "log" "mime/multipart" "net/http" "net/url" @@ -342,7 +343,10 @@ func parseResponseBody(c *Client, res *Response) (err error) { } if res.Request.Error != nil { - err = Unmarshalc(c, ct, res.body, res.Request.Error) + unmarshalErr := Unmarshalc(c, ct, res.body, res.Request.Error) + if unmarshalErr != nil { + log.Printf("[WARN] Cannot unmarshal response body: %s", unmarshalErr) + } } } } diff --git a/request_test.go b/request_test.go index 59809b44..726aa303 100644 --- a/request_test.go +++ b/request_test.go @@ -1744,6 +1744,54 @@ func TestHostHeaderOverride(t *testing.T) { logResponse(t, resp) } +type HTTPErrorResponse struct { + Error string `json:"error,omitempty"` +} + +func TestNotFoundWithError(t *testing.T) { + var httpError HTTPErrorResponse + ts := createGetServer(t) + defer ts.Close() + + resp, err := dc().R(). + SetHeader(hdrContentTypeKey, "application/json"). + SetError(&httpError). + Get(ts.URL + "/not-found-with-error") + + assertError(t, err) + assertEqual(t, http.StatusNotFound, resp.StatusCode()) + assertEqual(t, "404 Not Found", resp.Status()) + assertNotNil(t, resp.Body()) + assertEqual(t, "{\"error\": \"Not found\"}", resp.String()) + assertNotNil(t, httpError) + assertEqual(t, "Not found", httpError.Error) + + logResponse(t, resp) +} + +func TestNotFoundWithoutError(t *testing.T) { + var httpError HTTPErrorResponse + + ts := createGetServer(t) + defer ts.Close() + + c := dc().outputLogTo(os.Stdout) + resp, err := c.R(). + SetError(&httpError). + SetHeader(hdrContentTypeKey, "application/json"). + Get(ts.URL + "/not-found-no-error") + + assertError(t, err) + assertEqual(t, http.StatusNotFound, resp.StatusCode()) + assertEqual(t, "404 Not Found", resp.Status()) + assertNotNil(t, resp.Body()) + assertEqual(t, 0, len(resp.Body())) + assertNotNil(t, httpError) + assertEqual(t, "", httpError.Error) + + logResponse(t, resp) +} + func TestPathParamURLInput(t *testing.T) { ts := createGetServer(t) defer ts.Close() diff --git a/resty_test.go b/resty_test.go index 76d56fd2..5fe41a94 100644 --- a/resty_test.go +++ b/resty_test.go @@ -113,6 +113,13 @@ func createGetServer(t *testing.T) *httptest.Server { _, _ = w.Write(body) case "/host-header": _, _ = w.Write([]byte(r.Host)) + case "/not-found-with-error": + w.Header().Set(hdrContentTypeKey, "application/json") + w.WriteHeader(http.StatusNotFound) + _, _ = w.Write([]byte(`{"error": "Not found"}`)) + case "/not-found-no-error": + w.Header().Set(hdrContentTypeKey, "application/json") + w.WriteHeader(http.StatusNotFound) } switch { From 103590f8770ddf0d5a60928a6ba745d5bd3ba3aa Mon Sep 17 00:00:00 2001 From: Daniele Martinoli <86618610+dmartinol@users.noreply.github.com> Date: Tue, 25 Jul 2023 09:35:10 +0200 Subject: [PATCH 2/3] integrated reviewer's comment --- response.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/response.go b/response.go index 55012839..89fe214f 100644 --- a/response.go +++ b/response.go @@ -156,7 +156,7 @@ func (r *Response) setReceivedAt() { } func (r *Response) fmtBodyString(sl int64) string { - if r.body != nil { + if r.body != nil && len(r.body) > 0 { if int64(len(r.body)) > sl { return fmt.Sprintf("***** RESPONSE TOO LARGE (size - %d) *****", len(r.body)) } From 6de285e2767188cd1a1e447d663dde7cc3e8016f Mon Sep 17 00:00:00 2001 From: Daniele Martinoli <86618610+dmartinol@users.noreply.github.com> Date: Mon, 18 Sep 2023 14:46:10 +0200 Subject: [PATCH 3/3] Integrated comment --- middleware.go | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/middleware.go b/middleware.go index fa4ed092..9b19c463 100644 --- a/middleware.go +++ b/middleware.go @@ -9,7 +9,6 @@ import ( "errors" "fmt" "io" - "log" "mime/multipart" "net/http" "net/url" @@ -360,7 +359,7 @@ func parseResponseBody(c *Client, res *Response) (err error) { if res.Request.Error != nil { unmarshalErr := Unmarshalc(c, ct, res.body, res.Request.Error) if unmarshalErr != nil { - log.Printf("[WARN] Cannot unmarshal response body: %s", unmarshalErr) + c.log.Warnf("Cannot unmarshal response body: %s", unmarshalErr) } } }