Skip to content

Commit b46e6ec

Browse files
authored
Merge pull request #562 from jacksontj/issue_560
Add storage.Warnings to client
2 parents b5f6919 + d5f3c8d commit b46e6ec

File tree

4 files changed

+226
-154
lines changed

4 files changed

+226
-154
lines changed

api/client.go

Lines changed: 47 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,42 @@ 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+
}
63+
2864
// DefaultRoundTripper is used if no RoundTripper is set in Config.
2965
var DefaultRoundTripper http.RoundTripper = &http.Transport{
3066
Proxy: http.ProxyFromEnvironment,
@@ -55,14 +91,14 @@ func (cfg *Config) roundTripper() http.RoundTripper {
5591
// Client is the interface for an API client.
5692
type Client interface {
5793
URL(ep string, args map[string]string) *url.URL
58-
Do(context.Context, *http.Request) (*http.Response, []byte, error)
94+
Do(context.Context, *http.Request) (*http.Response, []byte, Error)
5995
}
6096

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

@@ -71,11 +107,14 @@ func DoGetFallback(c Client, ctx context.Context, u *url.URL, args url.Values) (
71107
u.RawQuery = args.Encode()
72108
req, err = http.NewRequest(http.MethodGet, u.String(), nil)
73109
if err != nil {
74-
return nil, nil, err
110+
return nil, nil, NewErrorAPI(err, nil)
75111
}
76112

77113
} else {
78-
return resp, body, err
114+
if err != nil {
115+
return resp, body, NewErrorAPI(err, nil)
116+
}
117+
return resp, body, nil
79118
}
80119
return c.Do(ctx, req)
81120
}
@@ -115,7 +154,7 @@ func (c *httpClient) URL(ep string, args map[string]string) *url.URL {
115154
return &u
116155
}
117156

118-
func (c *httpClient) Do(ctx context.Context, req *http.Request) (*http.Response, []byte, error) {
157+
func (c *httpClient) Do(ctx context.Context, req *http.Request) (*http.Response, []byte, Error) {
119158
if ctx != nil {
120159
req = req.WithContext(ctx)
121160
}
@@ -127,7 +166,7 @@ func (c *httpClient) Do(ctx context.Context, req *http.Request) (*http.Response,
127166
}()
128167

129168
if err != nil {
130-
return nil, nil, err
169+
return nil, nil, NewErrorAPI(err, nil)
131170
}
132171

133172
var body []byte
@@ -147,5 +186,5 @@ func (c *httpClient) Do(ctx context.Context, req *http.Request) (*http.Response,
147186
case <-done:
148187
}
149188

150-
return resp, body, err
189+
return resp, body, NewErrorAPI(err, nil)
151190
}

0 commit comments

Comments
 (0)