Skip to content

Commit

Permalink
fix: error return body
Browse files Browse the repository at this point in the history
Signed-off-by: Eray Ates <[email protected]>
  • Loading branch information
rytsh committed Jan 26, 2024
1 parent 367b3c5 commit 4eac411
Show file tree
Hide file tree
Showing 5 changed files with 52 additions and 2 deletions.
1 change: 1 addition & 0 deletions client.go
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,7 @@ func New(opts ...OptionClientFn) (*Client, error) {
RetryMax: o.RetryMax,
CheckRetry: o.RetryPolicy,
Backoff: o.Backoff,
ErrorHandler: PassthroughErrorHandler,
}

client = retryClient.StandardClient()
Expand Down
8 changes: 8 additions & 0 deletions policy.go
Original file line number Diff line number Diff line change
Expand Up @@ -131,3 +131,11 @@ func retryError(retry bool, err error, resp *http.Response, log logz.Adapter, er

return retry, fmt.Errorf("%w: [%s]", err, response)
}

func PassthroughErrorHandler(resp *http.Response, err error, _ int) (*http.Response, error) {
if resp == nil {
return nil, err
}

return resp, nil
}
2 changes: 1 addition & 1 deletion request.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ func DoWithInf[T any](ctx context.Context, client *http.Client, r Requester[T])
// Do sends an HTTP request and calls the response function with using http.Client.
func Do(c *http.Client, req *http.Request, fn func(*http.Response) error) error {
httpResp, err := c.Do(req)
if err != nil {
if httpResp == nil && err != nil {
return fmt.Errorf("%w: %w", ErrRequest, err)
}

Expand Down
37 changes: 36 additions & 1 deletion request_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ func TestClient_Request(t *testing.T) {
httpServer := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
// check request method
if r.Method != http.MethodPost {
w.WriteHeader(http.StatusBadRequest)
w.WriteHeader(http.StatusInternalServerError)
w.Write([]byte(`{"error": "invalid request method"}`))
return
}
Expand Down Expand Up @@ -64,6 +64,7 @@ func TestClient_Request(t *testing.T) {

client, err := New(
WithBaseURL(httpServer.URL),
WithRetryMax(1),
)
if err != nil {
t.Errorf("NewClient() error = %v", err)
Expand Down Expand Up @@ -128,6 +129,40 @@ func TestClient_Request(t *testing.T) {
},
wantErr: false,
},
{
name: "test-2",
fields: fields{
HttpClient: client.HTTP,
},
args: args{
ctx: context.Background(),
method: http.MethodGet,
path: "/api/v1/test",
body: bytes.NewBufferString(`{"id": "123"}`),
header: http.Header{
"X-Info": []string{"test"},
},
fn: func(resp *http.Response) error {
// check response status code
if resp.StatusCode != http.StatusInternalServerError {
return fmt.Errorf("invalid status code")
}

// get response body
var m map[string]interface{}
if err := json.NewDecoder(resp.Body).Decode(&m); err != nil {
return fmt.Errorf("invalid response body, %w", err)
}

if m["error"] != "invalid request method" {
return fmt.Errorf("invalid request error")
}

return nil
},
},
wantErr: false,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
Expand Down
6 changes: 6 additions & 0 deletions response.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package klient

import (
"bytes"
"encoding/json"
"fmt"
"io"
Expand Down Expand Up @@ -44,6 +45,11 @@ func LimitedResponse(resp *http.Response) []byte {

v, _ := io.ReadAll(io.LimitReader(resp.Body, ResponseErrLimit))

bodyRemains, _ := io.ReadAll(resp.Body)
totalBody := append(v, bodyRemains...)

resp.Body = io.NopCloser(bytes.NewReader(totalBody))

return v
}

Expand Down

0 comments on commit 4eac411

Please sign in to comment.