Skip to content

Commit

Permalink
Fix: avoid resp.Err been overridden
Browse files Browse the repository at this point in the history
  • Loading branch information
imroc committed Feb 24, 2023
1 parent be24aaa commit 868660c
Showing 1 changed file with 8 additions and 8 deletions.
16 changes: 8 additions & 8 deletions client.go
Original file line number Diff line number Diff line change
Expand Up @@ -1297,6 +1297,9 @@ func (c *Client) WrapRoundTrip(wrappers ...RoundTripWrapper) *Client {
// RoundTrip implements RoundTripper
func (c *Client) roundTrip(r *Request) (resp *Response, err error) {
resp = &Response{Request: r}
defer func() {
err = resp.Err
}()

// setup trace
if r.trace == nil && r.client.trace {
Expand All @@ -1322,8 +1325,8 @@ func (c *Client) roundTrip(r *Request) (resp *Response, err error) {

var reqBody io.ReadCloser
if r.GetBody != nil {
reqBody, err = r.GetBody()
if err != nil {
reqBody, resp.Err = r.GetBody()
if resp.Err != nil {
return
}
}
Expand Down Expand Up @@ -1369,21 +1372,18 @@ func (c *Client) roundTrip(r *Request) (resp *Response, err error) {
r.StartTime = time.Now()

var httpResponse *http.Response
httpResponse, err = c.httpClient.Do(r.RawRequest)
httpResponse, resp.Err = c.httpClient.Do(r.RawRequest)
resp.Response = httpResponse

// auto-read response body if possible
if err == nil && !c.disableAutoReadResponse && !r.isSaveResponse && !r.disableAutoReadResponse {
_, err = resp.ToBytes()
if resp.Err == nil && !c.disableAutoReadResponse && !r.isSaveResponse && !r.disableAutoReadResponse {
resp.ToBytes()
// restore body for re-reads
resp.Body = io.NopCloser(bytes.NewReader(resp.body))
} else if err != nil {
resp.Err = err
}

for _, f := range r.client.afterResponse {
if e := f(r.client, resp); e != nil {
err = e
resp.Err = e
return
}
Expand Down

0 comments on commit 868660c

Please sign in to comment.