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

Pass warnings through on non-error responses #599

Merged
merged 2 commits into from
Jun 13, 2019
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
56 changes: 11 additions & 45 deletions api/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,41 +25,7 @@ import (
"time"
)

func NewErrorAPI(err error, warnings []string) Error {
if err == nil && warnings == nil {
return nil
}
return &ErrorAPI{err, warnings}
}

type ErrorAPI struct {
err error
warnings []string
}

func (w *ErrorAPI) Err() error {
return w.err
}

func (w *ErrorAPI) Error() string {
if w.err != nil {
return w.err.Error()
}
return "Warnings: " + strings.Join(w.warnings, " , ")
}

func (w *ErrorAPI) Warnings() []string {
return w.warnings
}

// Error encapsulates an error + warning
type Error interface {
error
// Err returns the underlying error.
Err() error
// Warnings returns a list of warnings.
Warnings() []string
}
type Warnings []string

// DefaultRoundTripper is used if no RoundTripper is set in Config.
var DefaultRoundTripper http.RoundTripper = &http.Transport{
Expand Down Expand Up @@ -91,30 +57,30 @@ func (cfg *Config) roundTripper() http.RoundTripper {
// Client is the interface for an API client.
type Client interface {
URL(ep string, args map[string]string) *url.URL
Do(context.Context, *http.Request) (*http.Response, []byte, Error)
Do(context.Context, *http.Request) (*http.Response, []byte, Warnings, error)
}

// DoGetFallback will attempt to do the request as-is, and on a 405 it will fallback to a GET request.
func DoGetFallback(c Client, ctx context.Context, u *url.URL, args url.Values) (*http.Response, []byte, Error) {
func DoGetFallback(c Client, ctx context.Context, u *url.URL, args url.Values) (*http.Response, []byte, Warnings, error) {
req, err := http.NewRequest(http.MethodPost, u.String(), strings.NewReader(args.Encode()))
if err != nil {
return nil, nil, NewErrorAPI(err, nil)
return nil, nil, nil, err
}
req.Header.Set("Content-Type", "application/x-www-form-urlencoded")

resp, body, err := c.Do(ctx, req)
resp, body, warnings, err := c.Do(ctx, req)
if resp != nil && resp.StatusCode == http.StatusMethodNotAllowed {
u.RawQuery = args.Encode()
req, err = http.NewRequest(http.MethodGet, u.String(), nil)
if err != nil {
return nil, nil, NewErrorAPI(err, nil)
return nil, nil, warnings, err
}

} else {
if err != nil {
return resp, body, NewErrorAPI(err, nil)
return resp, body, warnings, err
}
return resp, body, nil
return resp, body, warnings, nil
}
return c.Do(ctx, req)
}
Expand Down Expand Up @@ -154,7 +120,7 @@ func (c *httpClient) URL(ep string, args map[string]string) *url.URL {
return &u
}

func (c *httpClient) Do(ctx context.Context, req *http.Request) (*http.Response, []byte, Error) {
func (c *httpClient) Do(ctx context.Context, req *http.Request) (*http.Response, []byte, Warnings, error) {
if ctx != nil {
req = req.WithContext(ctx)
}
Expand All @@ -166,7 +132,7 @@ func (c *httpClient) Do(ctx context.Context, req *http.Request) (*http.Response,
}()

if err != nil {
return nil, nil, NewErrorAPI(err, nil)
return nil, nil, nil, err
}

var body []byte
Expand All @@ -186,5 +152,5 @@ func (c *httpClient) Do(ctx context.Context, req *http.Request) (*http.Response,
case <-done:
}

return resp, body, NewErrorAPI(err, nil)
return resp, body, nil, err
}
4 changes: 2 additions & 2 deletions api/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,7 @@ func TestDoGetFallback(t *testing.T) {
client := &httpClient{client: *(server.Client())}

// Do a post, and ensure that the post succeeds.
_, b, err := DoGetFallback(client, context.TODO(), u, v)
_, b, _, err := DoGetFallback(client, context.TODO(), u, v)
if err != nil {
t.Fatalf("Error doing local request: %v", err)
}
Expand All @@ -169,7 +169,7 @@ func TestDoGetFallback(t *testing.T) {

// Do a fallbcak to a get.
u.Path = "/blockPost"
_, b, err = DoGetFallback(client, context.TODO(), u, v)
_, b, _, err = DoGetFallback(client, context.TODO(), u, v)
if err != nil {
t.Fatalf("Error doing local request: %v", err)
}
Expand Down
Loading