Skip to content

Commit

Permalink
Merge pull request #20 from bailantaotao/edwin/return-body-or-status-…
Browse files Browse the repository at this point in the history
…if-response-get-err

FIX: return status or body if response get error
  • Loading branch information
c9s authored Oct 17, 2023
2 parents 7be7617 + 7fbd7a8 commit 6c79921
Showing 1 changed file with 11 additions and 3 deletions.
14 changes: 11 additions & 3 deletions client.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,10 @@ import (
"bytes"
"context"
"encoding/json"
"fmt"
"net/http"
"net/url"
"time"

"github.com/pkg/errors"
)

type AuthenticatedRequestBuilder interface {
Expand Down Expand Up @@ -88,7 +87,7 @@ func (c *BaseAPIClient) SendRequest(req *http.Request) (*Response, error) {

// Check error, if there is an error, return the ErrorResponse struct type
if response.IsError() {
return response, errors.New(string(response.Body))
return response, &responseErr{Code: response.StatusCode, Body: response.Body}
}

return response, nil
Expand All @@ -111,3 +110,12 @@ func castPayload(payload interface{}) ([]byte, error) {

return nil, nil
}

type responseErr struct {
Code int
Body []byte
}

func (e *responseErr) Error() string {
return fmt.Sprintf("request failed with status code: %d, body: %q", e.Code, string(e.Body))
}

0 comments on commit 6c79921

Please sign in to comment.