@@ -25,41 +25,7 @@ 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
- }
28
+ type Warnings []string
63
29
64
30
// DefaultRoundTripper is used if no RoundTripper is set in Config.
65
31
var DefaultRoundTripper http.RoundTripper = & http.Transport {
@@ -91,30 +57,30 @@ func (cfg *Config) roundTripper() http.RoundTripper {
91
57
// Client is the interface for an API client.
92
58
type Client interface {
93
59
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 )
95
61
}
96
62
97
63
// 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 ) {
99
65
req , err := http .NewRequest (http .MethodPost , u .String (), strings .NewReader (args .Encode ()))
100
66
if err != nil {
101
- return nil , nil , NewErrorAPI ( err , nil )
67
+ return nil , nil , nil , err
102
68
}
103
69
req .Header .Set ("Content-Type" , "application/x-www-form-urlencoded" )
104
70
105
- resp , body , err := c .Do (ctx , req )
71
+ resp , body , warnings , err := c .Do (ctx , req )
106
72
if resp != nil && resp .StatusCode == http .StatusMethodNotAllowed {
107
73
u .RawQuery = args .Encode ()
108
74
req , err = http .NewRequest (http .MethodGet , u .String (), nil )
109
75
if err != nil {
110
- return nil , nil , NewErrorAPI ( err , nil )
76
+ return nil , nil , warnings , err
111
77
}
112
78
113
79
} else {
114
80
if err != nil {
115
- return resp , body , NewErrorAPI ( err , nil )
81
+ return resp , body , warnings , err
116
82
}
117
- return resp , body , nil
83
+ return resp , body , warnings , nil
118
84
}
119
85
return c .Do (ctx , req )
120
86
}
@@ -154,7 +120,7 @@ func (c *httpClient) URL(ep string, args map[string]string) *url.URL {
154
120
return & u
155
121
}
156
122
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 ) {
158
124
if ctx != nil {
159
125
req = req .WithContext (ctx )
160
126
}
@@ -166,7 +132,7 @@ func (c *httpClient) Do(ctx context.Context, req *http.Request) (*http.Response,
166
132
}()
167
133
168
134
if err != nil {
169
- return nil , nil , NewErrorAPI ( err , nil )
135
+ return nil , nil , nil , err
170
136
}
171
137
172
138
var body []byte
@@ -186,5 +152,5 @@ func (c *httpClient) Do(ctx context.Context, req *http.Request) (*http.Response,
186
152
case <- done :
187
153
}
188
154
189
- return resp , body , NewErrorAPI ( err , nil )
155
+ return resp , body , nil , err
190
156
}
0 commit comments