Skip to content

Commit 1335ef4

Browse files
jacksontjkrasi-georgiev
authored andcommitted
Pass warnings through on non-error responses (#599)
Return warnings as a separate string slice to simplify handling. Signed-off-by: Thomas Jackson <jacksontj.89@gmail.com>
1 parent e7f6132 commit 1335ef4

File tree

4 files changed

+241
-242
lines changed

4 files changed

+241
-242
lines changed

api/client.go

+11-45
Original file line numberDiff line numberDiff line change
@@ -25,41 +25,7 @@ import (
2525
"time"
2626
)
2727

28-
func NewErrorAPI(err error, warnings []string) Error {
29-
if err == nil && warnings == nil {
30-
return nil
31-
}
32-
return &ErrorAPI{err, warnings}
33-
}
34-
35-
type ErrorAPI struct {
36-
err error
37-
warnings []string
38-
}
39-
40-
func (w *ErrorAPI) Err() error {
41-
return w.err
42-
}
43-
44-
func (w *ErrorAPI) Error() string {
45-
if w.err != nil {
46-
return w.err.Error()
47-
}
48-
return "Warnings: " + strings.Join(w.warnings, " , ")
49-
}
50-
51-
func (w *ErrorAPI) Warnings() []string {
52-
return w.warnings
53-
}
54-
55-
// Error encapsulates an error + warning
56-
type Error interface {
57-
error
58-
// Err returns the underlying error.
59-
Err() error
60-
// Warnings returns a list of warnings.
61-
Warnings() []string
62-
}
28+
type Warnings []string
6329

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

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

105-
resp, body, err := c.Do(ctx, req)
71+
resp, body, warnings, err := c.Do(ctx, req)
10672
if resp != nil && resp.StatusCode == http.StatusMethodNotAllowed {
10773
u.RawQuery = args.Encode()
10874
req, err = http.NewRequest(http.MethodGet, u.String(), nil)
10975
if err != nil {
110-
return nil, nil, NewErrorAPI(err, nil)
76+
return nil, nil, warnings, err
11177
}
11278

11379
} else {
11480
if err != nil {
115-
return resp, body, NewErrorAPI(err, nil)
81+
return resp, body, warnings, err
11682
}
117-
return resp, body, nil
83+
return resp, body, warnings, nil
11884
}
11985
return c.Do(ctx, req)
12086
}
@@ -154,7 +120,7 @@ func (c *httpClient) URL(ep string, args map[string]string) *url.URL {
154120
return &u
155121
}
156122

157-
func (c *httpClient) Do(ctx context.Context, req *http.Request) (*http.Response, []byte, Error) {
123+
func (c *httpClient) Do(ctx context.Context, req *http.Request) (*http.Response, []byte, Warnings, error) {
158124
if ctx != nil {
159125
req = req.WithContext(ctx)
160126
}
@@ -166,7 +132,7 @@ func (c *httpClient) Do(ctx context.Context, req *http.Request) (*http.Response,
166132
}()
167133

168134
if err != nil {
169-
return nil, nil, NewErrorAPI(err, nil)
135+
return nil, nil, nil, err
170136
}
171137

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

189-
return resp, body, NewErrorAPI(err, nil)
155+
return resp, body, nil, err
190156
}

api/client_test.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -152,7 +152,7 @@ func TestDoGetFallback(t *testing.T) {
152152
client := &httpClient{client: *(server.Client())}
153153

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

170170
// Do a fallbcak to a get.
171171
u.Path = "/blockPost"
172-
_, b, err = DoGetFallback(client, context.TODO(), u, v)
172+
_, b, _, err = DoGetFallback(client, context.TODO(), u, v)
173173
if err != nil {
174174
t.Fatalf("Error doing local request: %v", err)
175175
}

0 commit comments

Comments
 (0)