Skip to content

Commit

Permalink
client: return status or body if response get error
Browse files Browse the repository at this point in the history
  • Loading branch information
bailantaotao committed Oct 17, 2023
1 parent db0948e commit 7fbd7a8
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 7fbd7a8

Please sign in to comment.