@@ -25,6 +25,42 @@ import (
25
25
"time"
26
26
)
27
27
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
+
28
64
// DefaultRoundTripper is used if no RoundTripper is set in Config.
29
65
var DefaultRoundTripper http.RoundTripper = & http.Transport {
30
66
Proxy : http .ProxyFromEnvironment ,
@@ -55,14 +91,14 @@ func (cfg *Config) roundTripper() http.RoundTripper {
55
91
// Client is the interface for an API client.
56
92
type Client interface {
57
93
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 )
59
95
}
60
96
61
97
// 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 ) {
63
99
req , err := http .NewRequest (http .MethodPost , u .String (), strings .NewReader (args .Encode ()))
64
100
if err != nil {
65
- return nil , nil , err
101
+ return nil , nil , NewErrorAPI ( err , nil )
66
102
}
67
103
req .Header .Set ("Content-Type" , "application/x-www-form-urlencoded" )
68
104
@@ -71,11 +107,14 @@ func DoGetFallback(c Client, ctx context.Context, u *url.URL, args url.Values) (
71
107
u .RawQuery = args .Encode ()
72
108
req , err = http .NewRequest (http .MethodGet , u .String (), nil )
73
109
if err != nil {
74
- return nil , nil , err
110
+ return nil , nil , NewErrorAPI ( err , nil )
75
111
}
76
112
77
113
} else {
78
- return resp , body , err
114
+ if err != nil {
115
+ return resp , body , NewErrorAPI (err , nil )
116
+ }
117
+ return resp , body , nil
79
118
}
80
119
return c .Do (ctx , req )
81
120
}
@@ -115,7 +154,7 @@ func (c *httpClient) URL(ep string, args map[string]string) *url.URL {
115
154
return & u
116
155
}
117
156
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 ) {
119
158
if ctx != nil {
120
159
req = req .WithContext (ctx )
121
160
}
@@ -127,7 +166,7 @@ func (c *httpClient) Do(ctx context.Context, req *http.Request) (*http.Response,
127
166
}()
128
167
129
168
if err != nil {
130
- return nil , nil , err
169
+ return nil , nil , NewErrorAPI ( err , nil )
131
170
}
132
171
133
172
var body []byte
@@ -147,5 +186,5 @@ func (c *httpClient) Do(ctx context.Context, req *http.Request) (*http.Response,
147
186
case <- done :
148
187
}
149
188
150
- return resp , body , err
189
+ return resp , body , NewErrorAPI ( err , nil )
151
190
}
0 commit comments