Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: parseResponseBody overrides original error code in case of unmar… #674

Merged
merged 4 commits into from
Sep 19, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
5 changes: 4 additions & 1 deletion middleware.go
Original file line number Diff line number Diff line change
Expand Up @@ -357,7 +357,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 {
c.log.Warnf("Cannot unmarshal response body: %s", unmarshalErr)
}
}
}
}
Expand Down
48 changes: 48 additions & 0 deletions request_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
7 changes: 7 additions & 0 deletions resty_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down